tomcat编译超过64k大小的jsp文件报错原因

 今天遇到一个问题,首先是在tomcat中间件上跑的web项目,一个jsp文件,因为代码行数实在是太多了,更新了几个版本之后编译报错了,页面打开都是报500的错误,500的报错,知道http协议返回码的都知道,这是服务端的报错。

jsp编译过程是先编译为servlet,然后再通过类加载器编译为.class文件,再执行为Servlet实例。这就是jsp的编译过程。所以jsp报500错误也可以理解,属于服务端的报错没什么好怀疑的。

服务端报错,肯定就是去console拿日志了。从CONSOLE拿到日志关键信息:

The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

这个报错意思大概是超过字节限制。通过网上资料搜索,很多地方都是给出了一个解决方法,不过大部分都没说明为什么。
网上一大堆差不多的博客,都是这样说的,在tomcat的conf文件夹里,找到web.xml,然后在JspServlet的servlet配置里,加上mappedfile参数
修改后的代码

<servlet>         <servlet-name>jsp</servlet-name>         <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>         <init-param>             <param-name>fork</param-name>             <param-value>false</param-value>         </init-param>         <init-param>             <param-name>xpoweredBy</param-name>             <param-value>false</param-value>         </init-param>         <init-param>               <param-name>mappedfile</param-name>               <param-value>false</param-value>           </init-param>          <load-on-startup>3</load-on-startup>     </servlet>

其实也就是加上

<init-param>              <param-name>mappedfile</param-name>              <param-value>false</param-value>    </init-param>

大部分博客并没有给出原因。不过还是可以解决问题的。不过网上所说的这种方法并不是很好的方法,只能说是暂缓之策。

首先要从jsp的编译说起,jsp经过tomcat编译后,文件会保存在哪里?
下面介绍一下,一般路径都会在${TOMCAT_HOME}\work\Catalina\localhost\项目名称\org\apache\jsp文件夹下面。
假如新建了一个index.jsp,经过编译之后,都会在该路径下面生成index_jsp.java文件和index_jsp.class文件,index_jsp.java文件是什么?其实可以理解为tomcat编译生成的servlet类,index_jsp.class呢?当然就是servlet类编译之后生成的.class文件了。
随便找个index_jsp.java文件,拿代码来看看:

/*  * Generated by the Jasper component of Apache Tomcat  * Version: Apache Tomcat/7.0.32  * Generated at: 2016-11-19 03:26:12 UTC  * Note: The last modified time of this file was set to  *       the last modified time of the source file after  *       generation to assist with modification tracking.  */ package org.apache.jsp;  import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.*;  public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase     implements org.apache.jasper.runtime.JspSourceDependent {    private static final javax.servlet.jsp.JspFactory _jspxFactory =           javax.servlet.jsp.JspFactory.getDefaultFactory();    private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;    private javax.el.ExpressionFactory _el_expressionfactory;   private org.apache.tomcat.InstanceManager _jsp_instancemanager;    public 
                    
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信