Reputation: 26548
We are creating session in our application after successful login of the user using the following code
HttpSession session = request.getSession(true);
sesssion.setAttribute("adminUserName", principalName);
Now it should create a cookie in the browser with JSESSIONID
as soon as we send the response and redirect to some other page which is a JSP. Now we are trying to print all the cookies from the request in the JSP by using the following code.
Cookie cookie = null;
Cookie[] cookies = null;
cookies = request.getCookies();
if (cookies != null) {
out.println("<h2> List of cookies : </h2>");
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
out.print("Name : " + cookie.getName() + ", ");
out.print("Value: " + cookie.getValue() + " <br/>");
}
} else {
out.println("<h2>No cookies founds</h2>");
}
but the result is No cookies founds and the problem it is creating is that, in the next servlet, we are getting the session from request object as null
.
What could be the possible cause?
Upvotes: 0
Views: 1789
Reputation: 364
I do not see anywhere where you add the cookie on your code.For example :
Cookie firstCookie= new Cookie("adminUserName",
request.getParameter("principalName"));
response.addCookie( firstCookie);
Second you get your session
request.getSession(false);
This should not create a new session just return the current one if exists.Otherwise return null.
Upvotes: 0