无法从ajax调用servlet检索会话属性

时间:2013-07-04 11:11:18

标签: java jquery servlets

我在通过servlet cal调用的ajax中检索会话属性时遇到问题。

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

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

CheckNoOfSearch servlet我正在尝试检索一些会话属性,但所有会话属性都为null但不是我没有设置它。

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();
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以直接在servlet中获取会话。

public class MyServlet extends HttpServlet {

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

答案 1 :(得分:1)

HttpSession由jsessionid识别,你必须使用Cookie头或URL重写将jsessionid传递给服务器。