ntstha
ntstha

Reputation: 1173

cannot retrieve session attributes from ajax call to servlet

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

Upvotes: 2

Views: 1348

Answers (2)

Septem
Septem

Reputation: 3622

HttpSession is identified by jsessionid, you have to pass jsessionid to the server using Cookie header or URL rewriting.

Upvotes: 1

Esteban S
Esteban S

Reputation: 1919

You can get the session in your servlet straight.

public class MyServlet extends HttpServlet {

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

Upvotes: 1

Related Questions