今天遇到一個問題,首先是在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.www.gcyl152.com apache.jasper.servlet.www.michenggw.com 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<www.tongqt178.com /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.www.mcyllpt.com 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.www.ysyl157.com yongshiyule178.com InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write('\r');
out.write('\n');
if (true) {
_jspx_page_context.forward("/login_toLogin");
return;
}
out.write('\r');
out.write('\n');
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
從代碼能夠看出,類繼承於HttpJspBase類實現JspSourceDependent接口,先看一下HttpJspBase類,這個類從哪來的呢?HttpJspBase是tomcat庫提供的,因此拿tomcat庫的源碼來看看,在${TOMCAT_HOME}/lib裏找到價包jasper.jar,反編譯代碼,找到HttpJspBase類
package org.apache.jasper.runtime;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
import org.apache.jasper.compiler.Localizer;
public abstract class HttpJspBase extends HttpServlet
implements HttpJspPage
{
private static final long serialVersionUID = 1L;
public final void init(ServletConfig config)
throws ServletException
{
super.init(config);
jspInit();
_jspInit();
}
public String getServletInfo()
{
return Localizer.getMessage("jsp.engine.info");
}
public final void destroy()
{
jspDestroy();
_jspDestroy();
}
public final void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
_jspService(request, response);
}
public void jspInit()
{
}
public void _jspInit()
{
}
public void jspDestroy()
{
}
protected void _jspDestroy()
{
}
public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException;
}
代碼並非說多複雜,HttpJspBase類繼承HttpServlet類,實現HttpJspPage接口,也就是說HttpJspBase重寫了HttpServlet的service(),init()等等方法,HttpServlet,咱們就很熟悉了。HttpJspPage又是什麼?看它的包名,立刻知道它是jdk提供的接口,立刻找到它的代碼:
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
*
* Portions Copyright Apache Software Foundation.
*/
package javax.servlet.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
/**
* The HttpJspPage interface describes the interaction that a JSP Page
* Implementation Class must satisfy when using the HTTP protocol.
*
* <p>
* The behaviour is identical to that of the JspPage, except for the signature
* of the _jspService method, which is now expressible in the Java type
* system and included explicitly in the interface.
*
* @see JspPage
*/
public interface HttpJspPage extends JspPage {
/** The _jspService()method corresponds to the body of the JSP page. This
* method is defined automatically by the JSP container and should never
* be defined by the JSP page author.
* <p>
* If a superclass is specified using the extends attribute, that
* superclass may choose to perform some actions in its service() method
* before or after calling the _jspService() method. See using the extends
* attribute in the JSP_Engine chapter of the JSP specification.
*
* @param request Provides client request information to the JSP.
* @param response Assists the JSP in sending a response to the client.
* @throws ServletException Thrown if an error occurred during the
* processing of the JSP and that the container should take
* appropriate action to clean up the request.
* @throws IOException Thrown if an error occurred while writing the
* response for this page.
*/
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException;
}
**很關鍵的方法名:_jspService,不就是剛纔CONSOLE報錯提示的方法名?
也就是說jdk提供接口,而後tomcat對接口進行實現,咱們知道Java內存模型(JMM)規定了一個方法的大小隻能是64k,因此,從剛纔的報錯,咱們簡單從源碼分析了一下,報錯的緣由其實就是jsp反編譯爲Servlet以後,代碼要通過_jspService這個方法,這個方法超過了64k,致使報錯。**
查看一下tomcat7官方給出的文檔:http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html#Configuration
找到mappedfile屬性的意思
mappedfile - 咱們是否應該爲每一個輸入行生成一個print語句的靜態內容,以便於調試? true或者false,默認true。
如今分析一下具體緣由。代碼報錯的緣由就是由於jsp編譯爲Servlet以後,通過_jspService這個方法,方法超過64k致使報錯。而後經過設置mappedfile參數的緣由是儘可能減小print代碼,暫時使代碼不超過,也就是說只是一種暫緩的方法。網上資料說經過html