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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Ubuntu16.04环境下PyTorch简易安装教程
该文介绍了在Ubuntu 16.04环境下安装NVIDIA GPU显卡驱动、CUDA 8.0以及PyTorch的方法。首先,需要更新系统并安装NVIDIA驱动,然后下载CUDA 8.0,接着安装PyTorch。安装完成后,可以通过在终端中输入 'import torch' 来验证安装是否成功。最后,更新numpy并验证GPU是否可用。
红色石头
2017/12/28
3.4K0
Ubuntu16.04环境下PyTorch简易安装教程
ubuntu16.04下NVIDIA GTX965M显卡驱动PPA安装
该文介绍了在Ubuntu 16.04系统中,安装NVIDIA GTX965M显卡驱动的方法,通过PPA源安装,禁用nouveau驱动,并更新内核,即可成功安装。安装完成后,重启系统,登录死机现象消失,系统运行正常。
10km
2018/01/03
9.8K1
ubuntu16.04下NVIDIA GTX965M显卡驱动PPA安装
Ubuntu下显卡驱动安装
安装完毕后跳出一个界面,选择lightdm,再sudo service lightdm stop。
数据科学工厂
2023/01/19
3.4K0
深度学习:Nvidia显卡驱动安装
安装完毕后跳出一个界面,选择lightdm,再sudo service lightdm stop。
数据科学工厂
2023/02/27
3.2K0
深度学习:Nvidia显卡驱动安装
.deb版本cuda安装。
1).run形式安装cuda。清理原有显卡驱动后,先安装自己显卡对应的驱动,在步骤中出现”Would you like to run the nvidia-xconfig utility to automatically update your X configuration file…”时,选择 No。(这里是cuda自带的旧版本的驱动)。
全栈程序员站长
2022/09/05
2.8K0
解决Ubuntu循环登陆问题 + Nvidia驱动、Cuda、Cudnn安装
ubuntu系统显卡驱动偶尔会出现奇怪的问题,造成图形用户界面循环登陆,本文记录相关问题的解决方案。 卸载旧驱动 在登陆界面 ctrl+alt+F2 进入非图形界面控制台,登陆后输入命令: sudo apt-get remove nvidia-* sudo apt-get autoremove sudo apt-get purge nvidia* 安装驱动 加入PPA,然后更新库 sudo add-apt-repository ppa:graphics-drivers sudo apt-get
为为为什么
2022/08/05
1K0
AIGC | 为机器学习工作站安装NVIDIA 4070 Ti Super显卡驱动
话接上篇《AIGC | Ubuntu24.04桌面版安装后必要配置》文章,作为作者进行机器学习的基础篇(筑基期),后续将主要介绍机器学习环境之如何在Ubuntu24.04桌面系统中进行NVIDIA显卡驱动安装,CUDA Toolkit安装,以及cuDNN的安装,以作者实践经历帮助读者快速搭建机器学习环境。
全栈工程师修炼指南
2024/07/05
6540
AIGC | 为机器学习工作站安装NVIDIA 4070 Ti Super显卡驱动
ubuntu16.04下安装NVIDIA(cuda)-gtx965m相关步骤以及问题
ubunt版本:16.04 笔记本:机械革命,i7-6700,gtx965m(集显Intel Hm170 ) 安装NVIDIA以及cuda来测试深度学习
老潘
2018/06/21
1.3K0
ubuntu16.04下安装NVIDIA(cuda)-gtx965m相关步骤以及问题
ubuntu16.04 显卡驱动与cuda安装
摘要总结:本文主要介绍了在Ubuntu 16.04上配置Nvidia显卡驱动的方法,包括安装前的准备、下载驱动、安装驱动和配置环境。同时,还介绍了如何安装CUDA和Cudnn,以及如何在Ubuntu 16.04上安装Bazel构建C++项目。
ke1th
2018/01/02
2.4K0
Ubuntu16安装Nvidia驱动(GTX1060显卡)
本篇概览 台式机是2018年购买的惠普暗隐精灵3代,显卡GTX1060,本文记录了此机器安装Ubuntu 16.04.7 LTS,再安装Nvidia驱动的过程; 另外还有一些避坑的小结,如果您遇到了类似问题可以拿来参考; 纯净Ubuntu系统 先安装Ubuntu16 LTS桌面版 U盘安装,我这里是惠普台式机,启动时出现惠普LOGO的时候,多次点击F10,进入bios,启动顺序选择U盘启动,然后在页面指导下顺利安装Ubuntu系统 需要注意的地方 网上很多安装文档中提到了要在BIOS设置中关闭secure
程序员欣宸
2021/12/07
8740
Ubuntu16安装Nvidia驱动(GTX1060显卡)
Ubuntu18.04安装 NVIDIA驱动+CUDA10.2+cuDNN+TensorRT
之后,按照提示安装,成功后重启即可。 如果提示安装失败,不要着急重启;可重复上述步骤,多试几次。
全栈程序员站长
2022/08/19
2K0
Ubuntu 16.04 下安装 NVIDIA GTX 970 显卡驱动
本文标题:《 Ubuntu 16.04 下安装 NVIDIA GTX 970 显卡驱动 》
zhonger
2022/10/28
1.2K0
ubuntu 16.04(Windows 10双系统+grub引导)无法进入tt1~tt6(NVIDIA驱动安装相关-黑屏,login loop,分辨率)
本来想了解一下X-Window,所以打算去tt1,结果ctrl+alt+f1出现的居然是图形界面,搜了一下是显卡驱动版本的问题,此时我用的是Xorg开源驱动,于是根据教程,准备安装一个最新版的NVIDIA驱动,然后噩梦就开始了QWQ 先回顾一下我的过程:
Enterprise_
2019/03/11
3.4K0
ubuntu 16.04(Windows 10双系统+grub引导)无法进入tt1~tt6(NVIDIA驱动安装相关-黑屏,login loop,分辨率)
手把手教你在 Ubuntu16.04 安装 GPU 驱动 + CUDA9.0 + cuDNN7
如果你的电脑安装了 Ubuntu16.04,而且电脑自带一块 NVIDIA GeForce 的 GPU 显卡,那么不用来跑深度学习模型就太可惜了!关于这方面的网上教程很多,但大都良莠不齐。这篇文章将手把手教你如何安装 GPU 显卡驱动、CUDA9.0 和 cuDNN7。值得一试!
红色石头
2022/01/12
6.8K0
手把手教你在 Ubuntu16.04 安装 GPU 驱动 + CUDA9.0 + cuDNN7
Ubuntu16.04安装Pytorch+CUDA
近年来,Pytorch深度学习框架由于其构建网络结构简单、入门门槛较低,越来越受到深度学习开发者的青睐,它与TensorFlow不同在于Pytorch是一个动态的框架,不需要一开始就定好了网络的架构,在运行期间可以边调试边修改,而TensorFlow则反之,这样带来的好处是开发者不需要一开始明确所构建网络的结构,可以慢慢学习找到更合适的结构,就好比在建筑工地的实地考察的工程师,工人们每搭一堵墙都会过来询问下一步要做什么,而TensorFlow就好比在办公室画图纸的建筑师,在施工之前就设计好整栋大楼的结构,而且设计时候也不会有人打扰,当然效率就比Pytorch要高了。
forrestlin
2018/11/13
6K0
双显卡笔电安装N卡驱动及CUDA
/由于工作需要,必须换操作系统了,一想到笔记本已经冗杂不堪,所以就索性重装成Linux系统,虽然显卡性能不如实验室的机器,但完全可以当做试验机,同时本身机子性能也不差,所以装个乌班图应该体验还不错。以上是我开始时的想法,后来装完了之后呢,体验总体也不错,但总归是有写麻烦,我总结一下放在开头。
ZONGLYN
2020/02/17
3.3K0
Ubuntu18.04LTS下cuda10.0+cudnn7.5+TensorFlow1.13环境搭建
前言 之前写过cuda环境的搭建文章, 这次干脆补全整个深度学习环境的搭建. ---- 开发环境一览 CPU: Intel core i7 4700MQ GPU: NVIDIA GT 750M
sean_yang
2019/03/15
1.9K0
Ubuntu18.04LTS下cuda10.0+cudnn7.5+TensorFlow1.13环境搭建
Ubuntu16.04下Nvidia+Cuda8.0+Dynet安装教程
Ubuntu16.04下Nvidia+Cuda8.0+Dynet安装教程 - WeiYang Bloggodweiyang.com
godweiyang
2020/03/24
6530
Ubuntu16.04下Nvidia+Cuda8.0+Dynet安装教程
在Ubuntu 18.04上安装NVIDIA
在安装之前首先就是要禁用Nouveau的驱动,禁用该驱动的方法参照这篇https://www.linuxidc.com/Linux/2019-02/157171.htm。
小陈运维
2021/10/13
1.3K0
Linux安装NVIDIA显卡驱动的正确姿势
可能想玩Linux系统的童鞋,往往死在安装NVIDIA显卡驱动上,所以这篇文章帮助大家以正常的方式安装NVIDIA驱动。
全栈程序员站长
2022/07/23
16.4K0
Linux安装NVIDIA显卡驱动的正确姿势
推荐阅读
相关推荐
Ubuntu16.04环境下PyTorch简易安装教程
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验