前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >tomcat系列之编译超过64k大小的jsp文件报错原因

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

作者头像
SmileNicky
发布于 2022-05-07 08:37:51
发布于 2022-05-07 08:37:51
98500
代码可运行
举报
文章被收录于专栏:Nicky's blogNicky's blog
运行总次数:0
代码可运行

今天遇到一个问题,首先是在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参数 修改后的代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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>

其实也就是加上

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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文件,拿代码来看看:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/*
 * 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类实现JspSourceDependent接口,先看一下HttpJspBase类,这个类从哪来的呢?HttpJspBase是tomcat库提供的,所以拿tomcat库的源码来看看,在${TOMCAT_HOME}/lib里找到价包jasper.jar,反编译代码,找到HttpJspBase类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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提供的接口,马上找到它的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/*
 * 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代码,暂时使代码不超过,也就是说只是一种暂缓的方法。网上资料说通过jsp:include方法或许可以,我并没有实践过,所以不讨论。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Zepto这样操作元素属性
已经知道了如何使用attr方法,在开始分析attr实现源码之前,我们先了解一下这几个函数。
IMWeb前端团队
2019/12/03
8210
Zepto这样操作元素属性
本文作者:IMWeb 谦龙 原文出处:IMWeb社区 未经同意,禁止转载 前言 使用Zepto的时候,我们经常会要去操作一些DOM的属性,或元素本身的固有属性或自定义属性等。比如常见的有a
IMWeb前端团队
2018/01/08
2.4K0
Zepto这样操作元素属性
Jquery的属性操作和DOM操作
       3 val()    :     获取或设置表单内容    (原生JS使用value)
用户3159471
2018/09/13
1.4K0
Jquery的属性操作和DOM操作
全面入门jQuery最佳实践(二)-jQuery的属性与样式1 .attr()与.removeAttr()2 html()及.text()
每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息。如:在img元素中,src就是元素的特性,用来标记图片的地址。
JavaEdge
2018/08/02
6940
03-老马jQuery教程-DOM操作(上)
根据文章内容总结摘要。
老马
2017/12/27
1.7K0
jQuery的基本操作
jQuery就是一个js的库· 主要分为两部分:            1·寻找元素         (选择器,筛选器)            2·操作元素          (CSS的操作,属性的操作,文本的处理) 选择器 基本  #id    #id //用于搜索的,通过元素的id属性中给定的值 描述:(查找ID为myDiv的元素) HTML代码: <div id="notMe"><p>id = "notMe"</p></div> <div id="myDiv">id="m
Wyc
2018/09/11
7.7K0
jQuery知识总结(最全 最精美)
使用: <script src="jquery-3.5.1/jquery-3.3.1.min.js"></script> 注意: 如果<script> 在body前面,应该使用 jQuery独有的预加载 $(function(){ 这里写代码 }) jQuery的基本设计思想和主要用法,就是"选择某个网页元素,然后对其进行某种操作"。 选择器: 所有选择器 * 标签选择器 标签名 ID选择器 #id 类选择器 .
编程张无忌
2021/01/26
4.8K0
前端基础-JQuery操作属性
第5章 JQuery操作属性 5.1 attr操作 设置单个属性 // 第一个参数:需要设置的属性名 // 第二个参数:对应的属性值 // $obj.attr(name, value); // 用法举例 $('img').attr('title','哎哟,不错哦'); $('img').attr('alt','哎哟,不错哦'); 设置多个属性 // 参数是一个对象,包含了需要设置的属性名和属性值 // $obj.attr(obj) // 用法举例 $('img').attr({ title:'哎哟,
cwl_java
2020/03/26
6940
【JQuery框架】超详细DOM操作看这一篇就够了!
之前和大家分享了jQuery框架中选择器的相关操作“【JQuery框架】五大选择器“全家桶”详解!!!”
灰小猿
2021/09/14
5610
jquery jQuery快速入门
维护IE678是一件让人头疼的事情,一般我们都会额外加载一个CSS和JS单独处理。值得庆幸的是使用这些浏览器的人也逐步减少,PC端用户已经逐步被移动端用户所取代,如果没有特殊要求的话,一般都会选择放弃对678的支持。
IT人一直在路上
2019/09/18
16.6K0
三、jQuery属性操作
2、如何操作属性 对象.属性名称 = 值 对象.属性名称 对象[‘属性名称’] = 值 对象[‘属性名称’] Javascript
Dreamy.TZK
2020/06/16
3.8K0
三、jQuery属性操作
jquery 属性&CSS操作 笔记
这是一个读写双用的方法,用来处理input的value,当方法没有参数的时候返回input的value值,当传递了一个参数的时候,方法修改input的value值为参数值
bamboo
2019/01/29
1.4K0
jquery 属性&CSS操作 笔记
前端之jQuery
jQuery对象就是通过jQuery包装DOM对象后产生的对象。jQuery对象是 jQuery独有的。如果一个对象是 jQuery对象,那么它就可以使用jQuery里的方法:例如$(“#i1”).html()。
GH
2019/12/16
5K0
jQuery.prop , jQuery.attr ,jQuery.data
jquery.prop 获取匹配的元素中第一个元素特定的属性值,或者是设置多个元素的属性值。
_淡定_
2018/08/24
4K0
JavaWeb——JQuery之DOM操作应用及实践案例总结(DOM内容操作、DOM属性操作、CRUD操作)
【注意】:prop和attr的区别,如果操作的是元素的固有属性(非自定义的),建议使用prop;若是自定义属性,建议用attr。
Winter_world
2020/09/25
3.1K0
JavaWeb——JQuery之DOM操作应用及实践案例总结(DOM内容操作、DOM属性操作、CRUD操作)
jQuery基础
一、jQuery是什么? jQuery是一个轻量级的、兼容多浏览器的JavaScript库。 jQuery使用户能够更方便地处理HTML Document、Events、实现动画效果、方便地进行Ajax交互,能够极大地简化JavaScript编程。它的宗旨就是:“Write less, do more.“ 二、为什么要用jQuery? 一款轻量级的JS框架。jQuery核心js文件才几十kb,不会影响页面加载速度。 丰富的DOM选择器,jQuery的选择器用起来很方便,比如要找到某个DO
人生不如戏
2018/04/12
2K0
WEB入门之十五 属性和样式
在前面几章的示例中,凡是牵涉到属性和样式操作的,我们依然是通过传统的DOM来实现的。本章将学习如何使用jQuery进行标签属性和CSS样式操作,jQuery提供了大量的函数来帮助开发人员简化对标签属性和CSS样式的操作。
张哥编程
2024/12/17
1480
WEB入门之十五  属性和样式
jquery
维护IE678是一件让人头疼的事情,一般我们都会额外加载一个CSS和JS单独处理。值得庆幸的是使用这些浏览器的人也逐步减少,PC端用户已经逐步被移动端用户所取代,如果没有特殊要求的话,一般都会选择放弃对678的支持。
suwanbin
2019/09/26
5.9K0
Jquery attr()方法 属性赋值和属性获取
jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr(),attr()有4个表达式。
软件小生活
2021/08/16
10.6K0
Jquery attr()方法 属性赋值和属性获取
jQuery介绍,一篇就够了!
老猫-Leo
2023/12/11
2390
相关推荐
Zepto这样操作元素属性
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验