2013-07-04 54 views
2

我在通過ajax cal調用servlet中檢索會話屬性時遇到問題。無法從ajax調用servlet檢索會話屬性

$('#homemainSearchField').submit(function(){ 

       $.get("./CheckNoOfSearch",function(data){ 
        checkLimitation(data); 
       }); 
      }); 

CheckNoOfSearchservlet我試圖找回一些會話屬性,但所有的會話屬性爲空,但它不是,我沒有把它設置。

的servlet代碼是

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     try { 
      HttpSession session = request.getSession(); 
      int noOfSearch = 0; 
      if (session.getAttribute("auth") != null && session.getAttribute("type") != null) { 
       System.out.println("Session found"); 
      } 

      out.print(noOfSearch); 
     } finally { 
      out.close(); 
     } 
    } 
+0

向我們展示您的servlet代碼。你的問題在於servlet代碼。您正確使用$ .get。 – Funkytown

回答

1

你可以在你的servlet直接獲取會話。

public class MyServlet extends HttpServlet { 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     HttpSession s = request.getSession(); 
    } 
} 
1

HttpSession由jsessionid標識,您必須使用Cookie標頭或URL重寫將jsessionid傳遞給服務器。