HELP: jsp cookie problem

Hi,

1) I have a page (index.jsp) that looks for a cookie, if the cookie isn't there it redirects to a login page.

2) The login page, on submit, calls a servlet that sets the cookie (when usr and pwd are valid). Then the servlet redirects to the original page (index.jsp).

3)The problem is, when redirected to the index.jsp, index.jsp cannot see the cookie -- so it redirects to login.jsp again. But if I refresh index.jsp it sees the cookie and works fine.

So it looks like the cookie isn't totally set between the addCookie in the servlet and the load of the index.jsp. Is there so kind of "commit" or something I'm supposed to do after the "addCookie" to make it take correctly?

Here are some code snippets -- any help is greatly appreciated!

# the servlet

response.addCookie(new Cookie("loggedin-username", sUsername));

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");

dispatcher.forward(request, response);

# the index.jsp

<%

Cookie[] cookies = request.getCookies();

boolean bLoggedIn = false;

int i=0;

while ( (cookies != null) && !bLoggedIn && (i<cookies.length) ) {

if (cookies.getName().equals("loggedin-username")) {

Cookie c = cookies;

bLoggedIn = (c.getValue() != null);

}

i++;

}

if (!bLoggedIn) {

response.sendRedirect("login.jsp");

}

%>

You are logged in.

[1525 byte] By [sleggea] at [2007-11-27 9:55:31]
# 1

>>>2) The login page, on submit, calls a servlet that sets the cookie (when usr and pwd are valid). Then the servlet redirects to the original page (index.jsp).

No the Servlet does a RequestDispatcher.forward to the index.jsp. A forward is a server side action that does not go to the browser and the target Servlet/JSP gets the original request and response objects.

So in the Servlet you set the cookie.

>>response.addCookie(new Cookie("loggedin-username", sUsername));

Notice that you set it in the response object.

Then the forward

>>dispatcher.forward(request, response);

And then the JSP looks for the cookie.

>>Cookie[] cookies = request.getCookies();

Notice it looks in the request for the cookies but since the response has not gone to the browser the browser has not had the opportunity to add the cookie into the request.

Then the forward

tolmanka at 2007-7-13 0:25:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks, tolmank. I see what you mean. In the JSP I'm looking for the cookie in the request.

Do you have any suggestions for how I can get at the cookie from the JSP? Unfortunately there doesn't appear to be a response.getCookie for me to get it that way.

Any further info or suggestions are appreciated!

(Actually, from your last sentence ("Then the forward"), it looks like your thought got cut short -- did you have more to add? I hope so!)

Thanks for the help.

sleggea at 2007-7-13 0:25:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Two options.

1: Actually do a response.sendRedirect instead of a forward. The redirect should go to the browser which should return the cookie in the request.

2: Change the logic in the the JSP to look for a cookie and if the cookie does not exist look for a request attribute. The Servlet would need to add the cookie and the request attribute before the forward.

tolmanka at 2007-7-13 0:25:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...