NullPointerException when setting attribute?

后端 未结 5 598
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 05:17

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

request.setAttribute(\"someValue\", someValue());
        RequestDispatcher rd         


        
相关标签:
5条回答
  • 2020-12-19 06:03
    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
    0 讨论(0)
  • 2020-12-19 06:03

    I'm not sure what this has to do with it being thread safe.

    The exception you're getting is a NullPointerException it looks like Tomcat is trying to invoke a method on a null object.

    In a Java web application each request has its own HttpServletRequest instance, so you can set the attributes on the request and be confident it is only for that user.

    0 讨论(0)
  • 2020-12-19 06:04

    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.

    0 讨论(0)
  • 2020-12-19 06:10

    use without this operator

         RequestDispatcher rd = getServletContext().getRequestDispatcher("/SomeJsp.jsp");
        rd.forward(request, response);
        return;
    
    0 讨论(0)
  • 2020-12-19 06:11

    If you look at implementaion of request interface in tomcat it looks like below code.

    public void setAttribute(String name, Object value) {
    
        // Name cannot be null
        if (name == null)
            throw new IllegalArgumentException
                (sm.getString("coyoteRequest.setAttribute.namenull"));
    
        // Null value is the same as removeAttribute()
        if (value == null) {
            removeAttribute(name);
            return;
        }
    
        if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
            internalDispatcherType = (DispatcherType)value;
            return;
        } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
            requestDispatcherPath = value;
            return;
        }
    

    Its clearly seen that if key is null then it throw IllegalArgumentException but if value is null then it simply remove the key and old associated object with that key from reposatory. But if none of them is null them it will associate the object with that key and add them o repository.

    It seems a temporary or tomcat issue.

    for more on implementation refer to below link Source code of Request Implementation by tomcat

    0 讨论(0)
提交回复
热议问题