Java 为什么servlet jsp的Servlet.service()会抛出这个异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2322031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:07:34  来源:igfitidea点击:

Why did Servlet.service() for servlet jsp throw this exception?

javajsp

提问by Terman

I get the following error, what could be the problem?

我收到以下错误,可能是什么问题?

My context descriptor:

我的上下文描述符:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <servlet>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>controller.UploadServlet</servlet-class>
  </servlet>
   <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/UploadServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)
    at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:159)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)
Feb 23, 2010 11:35:28 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)
    at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:159)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)

采纳答案by axtavt

It can be caused by a classpath contamination. Check that you /WEB-INF/libdoesn't contain something like jsp-api-*.jar.

它可能是由类路径污染引起的。检查您/WEB-INF/lib是否不包含类似jsp-api-*.jar.

回答by brabster

The problem is in your JSP, most likely you are calling a method on an object that is null at runtime.

问题出在您的 JSP 中,很可能您正在对运行时为 null 的对象调用方法。

It is happening in the _jspInit() call, which is a little more unusual... the problem code is probably a method declaration like <%! %>

它发生在 _jspInit() 调用中,这有点不寻常......问题代码可能是一个方法声明,如 <%! %>



Update: I've only reproduced this by overriding the _jspInit() method. Is that what you're doing? If so, it's not recommended - that's why it starts with an _.

更新:我只是通过覆盖 _jspInit() 方法来复制这个。那是你在做什么吗?如果是这样,则不推荐使用 - 这就是它以 _ 开头的原因。

回答by Ricket

I had this error; it happened somewhat spontaneously, and the page would halt in the browser in the middle of an HTML tag (not a section of code). It was baffling!

我有这个错误;它有点自发地发生,页面将在浏览器中的 HTML 标记(不是一段代码)中间停止。真是莫名其妙!

Turns out, I let a variable go out of scope and the garbage collector swept it away and then I tried to use it. Thus the seemingly-random timing.

事实证明,我让一个变量超出范围,垃圾收集器将其清除,然后我尝试使用它。因此,看似随机的时间。

To give a more concrete example... Inside a method, I had something like:

举一个更具体的例子......在一个方法中,我有类似的东西:

Foo[] foos = new Foo[20];
// fill up the "foos" array...
return Arrays.asList(foos); // this returns type List<Foo>

Now in my JSP page, I called that method and used the List object returned by it. The List object is backed by that "foos" array; but, the array went out of scope when I returned from the method (since it is a local variable). So shortly after returning, the garbage collector swept away the "foos" array, and my access to the List caused a NullPointerException since its underlying array was now wiped away.

现在在我的 JSP 页面中,我调用了该方法并使用了它返回的 List 对象。List 对象由“foos”数组支持;但是,当我从方法返回时,数组超出了范围(因为它是一个局部变量)。所以在返回后不久,垃圾收集器清除了“foos”数组,我对 List 的访问导致了 NullPointerException,因为它的底层数组现在被清除了。

I actually wondered, as I wrote the above method, whether that would happen.

我实际上想知道,当我编写上述方法时,是否会发生这种情况。

The even deeper underlying problem was premature optimization. I wanted a list, but I knew I would have exactly 20 elements, so I figured I'd try to be more efficient than new ArrayList<Foo>(20)which only sets an initial size of 20 but can possibly be less efficient than the method I used. So of course, to fix it, I just created my ArrayList, filled it up, and returned it. No more strange error.

更深层次的潜在问题是过早优化。我想要一个列表,但我知道我将有 20 个元素,所以我想我会尝试比new ArrayList<Foo>(20)仅将初始大小设置为 20 的方法更有效,但可能比我使用的方法效率低。所以当然,为了修复它,我刚刚创建了我的 ArrayList,填充它并返回它。没有更多奇怪的错误。

回答by bnguyen82

If your project is Maven-based, remember to set scope to provided for such dependencies as servlet-api, jsp-api. Otherwise, these jars will be exported to WEB-INF/lib and hence contaminate with those in Tomcat server. That causes painful problems.

如果您的项目是基于 Maven 的,请记住为 servlet-api、jsp-api 等依赖项设置作用域。否则,这些 jars 将被导出到 WEB-INF/lib 并因此污染 Tomcat 服务器中的那些。这会导致痛苦的问题。

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

回答by pkm1986

I tried my best to follow the answers given above. But I have below reason for the same.

我尽力遵循上面给出的答案。但我有以下相同的原因。

Note: This is for maven+eclipse+tomcat deployment and issue faced especially with spring mvc.

注意:这是针对 maven+eclipse+tomcat 部署和尤其是 spring mvc 面临的问题。

1- If you are including servlet and jsp dependency please mark them provided in scope.

1- 如果您包含 servlet 和 jsp 依赖项,请在范围内标记它们。

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
  1. Possibly you might be including jstl as dependency. So, jsp-api.jarand servlet-api.jarwill be included along. So, require to exclude the servlet-api and jsp-api being deployed as required lib in target or in "WEB-INF/lib" as given below.

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl-api</artifactId>
        <version>1.2</version>
        <exclusions>
            <exclusion>
                <artifactId>servlet-api</artifactId>
                <groupId>javax.servlet</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jsp-api</artifactId>
                <groupId>javax.servlet.jsp</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    
  1. 可能您可能将 jstl 作为依赖项包含在内。所以,jsp-api.jarservlet-api.jar将包括一起。因此,需要排除在目标或“WEB-INF/lib”中作为必需库部署的 servlet-api 和 jsp-api,如下所示。

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl-api</artifactId>
        <version>1.2</version>
        <exclusions>
            <exclusion>
                <artifactId>servlet-api</artifactId>
                <groupId>javax.servlet</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jsp-api</artifactId>
                <groupId>javax.servlet.jsp</groupId>
            </exclusion>
        </exclusions>
    </dependency>