例如,我有一个将属性设置为 HttpServletRequest 的 servlet 代码:
request.setAttribute("someValue", someValue());
RequestDispatcher rd = getServletContext().getRequestDispatcher("/SomeJsp.jsp");
rd.forward(this.request, this.response);
return;
如何确保上面的代码是线程安全的?
这是我得到的堆栈跟踪:
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)