前往小程序,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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
xmpp即时通讯三
6.1 概述       XMPP包含一个认证流的方法,此方法依靠一个简单认证与安全层(SASL)协议[SASL]的XMPP-specific profile。SASL提供一个一般化方法,用于给基于连接的协议加认证支持,并且,XMPP使用一个一般化XML命名空间profile,用于 SASL,遵从[SASL]的profiling需求。       以下规则应用:       1) 如果两个服务器间发生SASL协商,直到由服务器宣称的域名系统(DNS)主机名被解析了(参考服务器到服务器通信(14.4))
xiangzhihong
2018/01/29
2.1K0
xmpp即时通讯四
     TLS协商(5节)后,如果需要SASL协商(6节)与资源绑定(7节),XML节可通过流来发送。定义了三种XML节用于 'jabber:client'与'jabber:server'命名空间:<message/>, <presence/>, and <iq/>。另外,这种节有五个通用属性。这些通用属性,像三种节的基本语义一样,都定义在此;与即时消息与表示应用相关的XML节的更详细信息在[XMPP-IM]中提供。 9.1通用属性       以下五个属性对message, presence与IQ均
xiangzhihong
2018/01/29
2.2K0
xmpp即时通讯详解
摘要:         此文档定义了可扩展消息出席协议(XMPP)的核心特性:协议使用XML元素在任意两个网络端点间近实时的交换结构化信息。当XMPP为交换XML数据提供一般化,可扩展的框架时,它主要用于建立满足RFC2779的即时消息与出席应用的需求。 1 介绍 1.1 概要         XMPP是一个开放的可扩展标记语言[XML]协议,用于近实时的消息、出席与请求-响应服务。基本语法语义最初是由Jabber开源社区在1999年开发的。2002年,XMPP工作组授权开发一个Jabber协议的改
xiangzhihong
2018/01/29
3.1K0
熬了两个夜 写出来的功能 在小程序中使用websocket的xmpp协议 链接 openfire服务,构建一个简单的IM系统 demo
demo 下载 https://download.csdn.net/download/github_35631540/12423282 初生牛犊不怕虎 山重水复疑无路 柳暗花明又一村 为伊消得人憔悴 提携玉龙为君死 let WebIM = require("../../utils/WebIM")["default"]; let __test_account__, __test_psword__; let disp = require("../../utils/broadcast"); var X2JS =
拿我格子衫来
2022/01/24
7000
7.xmpp版即时聊天
即时聊天的解决方案 socket: xmpp:xmpp+openfire+asmack 环信 常见协议 比较安全,tcp上还加了俩层 简单聊一下socket socket:套接字,连接需要ip和端口,
六月的雨
2018/05/14
2.2K0
史上最全Web端即时通讯技术原理详解
关于IM(InstantMessaging)即时通信类软件(如微信,QQ),大多数都是桌面应用程序或者native应用较为流行,而网上关于原生IM或桌面IM软件类的通信原理介绍也较多,此处不再赘述。而web端的IM应用,由于浏览器的兼容性以及其固有的“客户端请求服务器处理并响应”的通信模型,造成了要在浏览器中实现一个兼容性较好的IM应用,其通信过程必然是诸多技术的组合,本文的目的就是要详细探讨这些技术并分析其原理和过程。 1.基于web的固有通信方式 浏览器本身作为一个瘦客户端,不具备直接通过系统调用来达
用户1263954
2018/06/22
1.8K0
XMPP协议
XMPP(Extensible Messageing and Presence Protocol)可扩展消息与存在协议,是目前主流的四种IM(即时消息)协议之一,其它三种分别为:即时信息和空间协议(IMPP)、空间和即时信息协议(PRIM)、针对即时通讯和空间平衡扩充的进程开始协议SIP(SIMPLE)。 在这四种协议中,XMPP是最灵活的。
阳光岛主
2019/02/19
3.2K0
基于XMPP协议的Android即时通信系
设计基于开源的XMPP即时通信协议,采用C/S体系结构,通过GPRS无线网络用TCP协议连接到服务器,以架设开源的Openfn'e服务器作为即时通讯平台。         系统主要由以下部分组成:一是服务器,负责管理发出的连接或者与其他实体的会话,接收或转发XML(ExtensibleMarkup Language)流元素给授权的客户端、服务器等;二是客户终端。它与服务器相连,通过XMPP获得由服务器或任何其它相关的服务所提供的全部功能。三是协议网关。完成XMPP协议传输的信息与外部消息系统可识别信息间的
xiangzhihong
2018/01/29
3.4K1
即时通讯软件openfire+spark+smack
所以我基本上分为三篇文章来介绍此类软件的开发: 第一篇是关于XMPP 协议是啥,IM 是啥以及一个比较有名的开源实现,该开源实现包括三个部分(Spark、Smack和Openfire); 第二篇讲如何开发基于Spark 的客户端IM 插件部分; 第三篇讲如何开发基于Openfire 服务器端的插件部分。 好了,进入正题吧。 什么是XMPP? Extensible Messaging and Presence Protocol,简单的来讲,它就是一个发送接收处理消息的协议,但是这个协议发送的消息,既不是二进
xiangzhihong
2018/01/30
2.3K0
即时通讯软件openfire+spark+smack
iOS XMPP系列教程(Swift)二 框架介绍
XMPPStream是xmpp的核心,这是你需要接触的主要类,所有的拓展类和定制类需要添加它,它由一些有趣的功能设计,让框架灵活、可拓展和更加容易在它之上开发
码客说
2019/10/22
9190
iOS 即时通讯 + 仿微信聊天框架 + 源码
更新:2017年8月1日 实在是抱歉,git上的Demo这么久,有问题自己没有发现!肯定给大家造成过不方便,抱歉!git上Demo刚重新上传,要有需要的可以去下载,要有问题可以直接找我QQ联系我!       最近在总结Socket方面的知识,等文章写完会发不来,这篇文章是去年什么四五月份写的吧,那是水平也是有限,希望接下来能总结的比这篇好一点!!       在Demo中,XMPP接收发送消息这一块的代码我暂时是删除了,仿照微信的聊天框架是在的,你要想通过XMPP自己在项目中试试,你需要的也就是在Dem
Mr.RisingSun
2018/02/06
4K0
iOS  即时通讯 + 仿微信聊天框架 + 源码
(建议精读)HTTP灵魂之问,巩固你的 HTTP 知识体系
上回就已经承诺过大家,一定会出 HTTP 的系列文章,今天终于整理完成了。作为一个 web 开发,HTTP 几乎是天天要打交道的东西,但我发现大部分人对 HTTP 只是浅尝辄止,对更多的细节及原理就了解不深了,在面试的时候感觉非常吃力。这篇文章就是为了帮助大家树立完整的 HTTP 知识体系,并达到一定的深度,从容地应对各种灵魂之问,也同时提升自己作为一个 web 开发的专业素养吧。这是本文的思维导图:
桃翁
2020/04/07
1.1K0
(建议精读)HTTP灵魂之问,巩固你的 HTTP 知识体系
通讯协议与即时通讯[通俗易懂]
客户端不断的查询服务器,检索新内容。这种方式的缺点十分明显,如果轮询频率过快,会大量消耗网络带宽和电池;
全栈程序员站长
2022/08/05
2.3K0
IM即时通讯实现原理
即时通讯(Instant Messenger,简称IM)软件多是基于TCP/IP和UDP进行通讯的,TCP/IP和UDP都是建立在更低层的IP协议上的两种通讯传输协议。前 者是以数据流的形式,将传输数据经分割、打包后,通过两台机器之间建立起的虚电路,进行连续的、双向的、严格保证数据正确性的文件传输协议。而后者是以数 据报的形式,对拆分后的数据的先后到达顺序不做要求的文件传输协议。 QQ就是使用UDP协议进行发送和接收消息的。当你的机器安装了OICQ以后,实际上,你既是服务端(Server),又是客户端(C
李海彬
2018/03/22
7.7K0
XMPP(一)-openfire服务端的安装和搭建
XMPP全称:可扩展通讯和表示协议 简介:可扩展通讯和表示协议 (XMPP) 可用于服务类实时通讯、表示和需求响应服务中的XML数据元流式传输。XMPP以Jabber协议为基础,而Jabber是即时通讯中常用的开放式协议。XMPP is the IETF's formalization of the base XML streaming protocols for instant messaging and presence developed within the Jabber open-sourc
xiangzhihong
2018/01/30
2K0
XMPP(一)-openfire服务端的安装和搭建
基于xmpp openfire smack开发之openfire介绍和部署[1]
http://blog.csdn.net/shimiso/article/details/8816558
bear_fish
2018/09/20
1.8K0
基于xmpp openfire smack开发之openfire介绍和部署[1]
终、《图解HTTP》读书笔记 - 汇总篇(总结)
又一本网络基础的书啃完了,这本书建议结合[[《网络是怎么样连接的》读书笔记 - 汇总篇]]这一篇读书笔记食用(当然也可以直接看原书)。
阿东
2022/12/06
6620
终、《图解HTTP》读书笔记 - 汇总篇(总结)
洞察Tungsten Fabric内部的XMPP
XMPP是一个用于通讯和表示的开放标准协议。从本质上讲,它是一个允许实体交换信息和进行聊天的协议。
Tungsten Fabric
2021/01/07
1.1K0
洞察Tungsten Fabric内部的XMPP
HTTP/2 探索第一篇:概念
小时光
2016/09/28
3.8K0
HTTP/2 探索第一篇:概念
iOS XMPP系列教程(Swift)三 代码实战
初始化流转图 代码 class AppDelegate: UIResponder, UIApplicationDelegate ,XMPPStreamDelegate,XMPPRosterDeleg
码客说
2019/10/22
5410
相关推荐
xmpp即时通讯三
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验