2017-06-11 57 views
-1

我正在通過ajax將json數據從jsp發送到servlet以存儲到數據庫中。我在該servlet頁面中設置會話值以捕獲其他jsp頁面上的會話值。但服務器日誌顯示異常記錄。在servlet中設置會話值時發生錯誤

這是我設置的會議方式:

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) 
+0

你可以分享你的FBBackedServlet類嗎? – Viraj

回答

1

此代碼:

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

是保證生成的一個NullPointerException第二行,如果沒有預先存在的與特定b關聯的對象HttpSession在請求中返回的rowser會話(通常通過名爲JSESSIONID的Cookie)。

在大多數情況下,您應該只使用request.getSession(),以便在必要時爲您創建HttpSession對象。

+0

會話只能在Servlet內部使用。當我嘗試在jsp中調用它時,使用<%= session.getAttribute(「userId」)%>,它將打印空 – Ilham

+0

這隻會發生在您的servlet尚未在某個時候被客戶端調用的情況。 –

相關問題