2

I am having problem while retrieving session attributes in servlet called via ajax cal.

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

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

In CheckNoOfSearch servlet i am trying to retrieve some session attributes, but all session attributes are null but its not that i haven't set it.

The servlet code is

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

2 回答 2

1

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

public class MyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession s = request.getSession();
    }
}
于 2013-07-04T11:27:05.780 回答
1

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

于 2013-07-04T12:39:47.967 回答