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

后端 未结 5 1369
闹比i
闹比i 2020-11-27 17:34

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

My context descriptor:




        
相关标签:
5条回答
  • 2020-11-27 17:53

    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.

    <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>
    
    0 讨论(0)
  • 2020-11-27 17:57

    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!

    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.

    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.

    0 讨论(0)
  • 2020-11-27 17:58

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

    0 讨论(0)
  • 2020-11-27 18:06

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

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


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

    0 讨论(0)
  • 2020-11-27 18:17

    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.

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

    <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.jar and servlet-api.jar will 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>
      
    0 讨论(0)
提交回复
热议问题