2016-09-26 133 views
0

我正在使用HtppServlet,我需要使用該會話才能知道我必須做什麼(第一次或沒有)。但是當我在會議中時,看起來總是不一樣的,所以這個過程總是和第一次應該做的一樣。下面的代碼:HttpSession.getSession()。isNew()始終爲真

 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException { 

       HttpSession session = request.getSession(true); 
       response.setContentType("application/json"); 
       PrintWriter out = response.getWriter(); 

       try { 
         if (session.isNew()) { 
          String id=callMethod1(); 
          session.setAttribute("ID",id); 
         } else { 
          callMethod2(session.getAttribute("ID")); 
         } 
       }catch (Exception ex) { 
       //Error handler 
       } finally { 
       out.close(); 
      } 
     } 

的事情是我需要調用callMethod2()第二次,但它始終在呼喚callMethod1(),我已經使用session.getSession(false)session.getSession()我使用捲曲試圖調用Servlet和谷歌Chrome,有沒有人有一個想法發生了什麼,或者我該如何解決這個問題?

回答

1

當您使用curl時,您必須發回您收到的cookies。最重要的cookie是標識會話的JSESSIONID cookie。如果您不發送,則服務器無法知道該請求是某個會話的一部分。

這裏是關於捲曲和餅乾一個非常好的教程:https://curl.haxx.se/docs/http-cookies.html

+0

嗯,我試圖發送Cookie,但它似乎沒有工作,要麼我得到一個不同的會話 – Alex

+0

您必須發送您在收到的JSESSIONID的cookie第一個請求的響應。 – gsl