tomcat編譯超過64k大小的jsp文件報錯原因

今天遇到一個問題,首先是在tomcat中間件上跑的web項目,一個jsp文件知态,因為代碼行數(shù)實在是太多了凰浮,更新了幾個版本之后編譯報錯了励烦,頁面打開都是報500的錯誤,500的報錯城菊,知道http協(xié)議返回碼的都知道秘蛇,這是服務端的報錯其做。

jsp編譯過程是先編譯為servlet,然后再通過類加載器編譯為.class文件赁还,再執(zhí)行為Servlet實例妖泄。這就是jsp的編譯過程。所以jsp報500錯誤也可以理解艘策,屬于服務端的報錯沒什么好懷疑的蹈胡。

服務端報錯,肯定就是去console拿日志了朋蔫。從CONSOLE拿到日志關鍵信息:

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

這個報錯意思大概是超過字節(jié)限制罚渐。通過網(wǎng)上資料搜索,很多地方都是給出了一個解決方法驯妄,不過大部分都沒說明為什么荷并。
網(wǎng)上一大堆差不多的博客,都是這樣說的富玷,在tomcat的conf文件夾里璧坟,找到web.xml既穆,然后在JspServlet的servlet配置里,加上mappedfile參數(shù)
修改后的代碼

<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>

大部分博客并沒有給出原因雀鹃。不過還是可以解決問題的幻工。不過網(wǎng)上所說的這種方法并不是很好的方法,只能說是暫緩之策黎茎。

首先要從jsp的編譯說起囊颅,jsp經(jīng)過tomcat編譯后,文件會保存在哪里傅瞻?
下面介紹一下踢代,一般路徑都會在${TOMCAT_HOME}\work\Catalina\localhost\項目名稱\org\apache\jsp文件夾下面。
假如新建了一個index.jsp嗅骄,經(jīng)過編譯之后胳挎,都會在該路徑下面生成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 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類實現(xiàn)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類括眠,實現(xiàn)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對接口進行實現(xiàn),我們知道Java內(nèi)存模型(JMM)規(guī)定了一個方法的大小只能是64k缩幸,所以壹置,從剛才的報錯竞思,我們簡單從源碼分析了一下,報錯的原因其實就是jsp反編譯為Servlet之后钞护,代碼要經(jīng)過_jspService這個方法盖喷,這個方法超過了64k,導致報錯难咕。

查看一下tomcat7官方給出的文檔:http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html#Configuration
找到mappedfile屬性的意思

mappedfile - 我們是否應該為每個輸入行生成一個print語句的靜態(tài)內(nèi)容课梳,以便于調試? true或者false余佃,默認true暮刃。

現(xiàn)在分析一下具體原因。代碼報錯的原因就是因為jsp編譯為Servlet之后爆土,經(jīng)過_jspService這個方法椭懊,方法超過64k導致報錯。然后通過設置mappedfile參數(shù)的原因是盡量減少print代碼雾消,暫時使代碼不超過灾搏,也就是說只是一種暫緩的方法挫望。網(wǎng)上資料說通過<jsp:include>方法或許可以立润,我并沒有實踐過,所以不討論媳板。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末桑腮,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蛉幸,更是在濱河造成了極大的恐慌破讨,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奕纫,死亡現(xiàn)場離奇詭異提陶,居然都是意外死亡,警方通過查閱死者的電腦和手機匹层,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門隙笆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人升筏,你說我怎么就攤上這事撑柔。” “怎么了您访?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵铅忿,是天一觀的道長。 經(jīng)常有香客問我灵汪,道長檀训,這世上最難降的妖魔是什么柑潦? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮峻凫,結果婚禮上妒茬,老公的妹妹穿的比我還像新娘。我一直安慰自己蔚晨,他們只是感情好乍钻,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著铭腕,像睡著了一般银择。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上累舷,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天浩考,我揣著相機與錄音,去河邊找鬼被盈。 笑死析孽,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的只怎。 我是一名探鬼主播袜瞬,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼身堡!你這毒婦竟也來了邓尤?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贴谎,失蹤者是張志新(化名)和其女友劉穎汞扎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體擅这,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡澈魄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了仲翎。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痹扇。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖谭确,靈堂內(nèi)的尸體忽然破棺而出帘营,到底是詐尸還是另有隱情,我是刑警寧澤逐哈,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布芬迄,位于F島的核電站,受9級特大地震影響昂秃,放射性物質發(fā)生泄漏禀梳。R本人自食惡果不足惜杜窄,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望算途。 院中可真熱鬧塞耕,春花似錦、人聲如沸嘴瓤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽廓脆。三九已至筛谚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間停忿,已是汗流浹背驾讲。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留席赂,地道東北人吮铭。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像颅停,于是被迫代替她去往敵國和親谓晌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容