2013-07-04 43 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传递给服务器。