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.

