Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when setting attribute?

Tags:

java

jsp

servlets

For example I have a servlet code that sets attribute to a HttpServletRequest:

request.setAttribute("someValue", someValue());
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/SomeJsp.jsp");
        rd.forward(this.request, this.response);
        return;

How do I make sure that the code above is thread safe?

This is the stacktrace I am getting:

java.lang.NullPointerException
    at org.apache.catalina.connector.Request.notifyAttributeAssigned(Request.java:1552)
    at org.apache.catalina.connector.Request.access$000(Request.java:105)
    at org.apache.catalina.connector.Request$3.set(Request.java:3342)
    at org.apache.catalina.connector.Request.setAttribute(Request.java:1504)
    at org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:541)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:286)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at com.mycompany.myapp.servlet.SomeServlet.doRequest(SomeServlet.java:103)
    at com.mycompany.myapp.servlet.SomeServlet.doGet(SomeServlet.java:159)
like image 209
quarks Avatar asked Dec 20 '12 11:12

quarks


2 Answers

rd.forward(this.request, this.response);

This (pun intented) suggests that you've assigned request and response as instance variables of a class. Your concrete problem in turn suggests that the instance of the class itself is not threadsafe.

Assuming that it's actually the servlet itself, then you've there the cause of your problem. Servlets are not threadsafe at all. There's only one instance of it been created during webapp's startup which is then shared across all requests applicationwide.

You should never assign request or session scoped data as an instance variable of the servlet. It would only be overriden whenever another HTTP request takes place at the same moment. That would make your code threadunsafe, as you encountered yourself.

Here's some code which illustrates that:

public class MyServlet extends HttpServlet {

    private Object thisIsNOTThreadSafe;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object thisIsThreadSafe;

        thisIsNOTThreadSafe = request.getParameter("foo"); // BAD!! Shared among all requests!
        thisIsThreadSafe = request.getParameter("foo"); // OK, this is thread safe.
    } 
}

Assigning the HTTP request itself as instance variable of a servlet is actually an epic mistake. When user Y fires another request at the same time the servlet is dealing with the request of user X, then user X would instantly get the request and response objects of user Y at hands. This is definitely threadunsafe. The NPE is caused because the request is at that moment "finished" with processing for user Y and thus released/destroyed.

See also:

  • How do servlets work? Instantiation, sessions, shared variables and multithreading
like image 146
BalusC Avatar answered Sep 18 '22 14:09

BalusC


Request is thread safe by it's definition (unlike Session and ServletContext).

Regarding the exception: what Tomcat version are you using? This looks like a Tomcat bug.

Does the class that is the return type of someValue() method implement HttpAttributeBindingListener? And, can someValue() method return null? If both yes then the NullPointerException is obvious.

like image 25
Igorry Avatar answered Sep 18 '22 14:09

Igorry