在servlet中设置会话值时引发错误

时间:2017-06-11 06:00:13

标签: ajax jsp session exception servlets

我通过ajax将json数据从jsp发送到servlet,以便存储到数据库中。我在该servlet页面中设置会话值以捕获其他jsp页面上的会话值。但服务器日志显示异常记录。

这是我设置会话的方式:

HttpSession session = request.getSession(false);
         session.setAttribute("userId", "123");

在服务器日志中:

11-Jun-2017 11:09:50.632 SEVERE [http-nio-80-exec-201] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [FBBackendServlet] in context with path [] threw exception
 java.lang.NullPointerException
    at Main.FBBackendServlet.processRequest(FBBackendServlet.java:236)
    at Main.FBBackendServlet.doPost(FBBackendServlet.java:274)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)

1 个答案:

答案 0 :(得分:1)

此代码:

 HttpSession session = request.getSession(false);
 session.setAttribute("userId", "123");
如果没有与请求中返回的特定浏览器会话相关联的预先存在的HttpSession对象(通常通过名为JSESSIONID的Cookie,则

保证在第二行生成NullPointerException。

在大多数情况下,您应该只使用request.getSession(),以便在必要时为您创建HttpSession对象。