Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm truly sorry, this is the second or third question about cookies, but honestly I can't understand why this happens...

On the login page I set the cookie this way:

Cookie cookie = new Cookie("username", user);
cookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cookie);


And then user is redirected to a different page.

I deleted almost all my code (to make sure there are no conflicts), except this:
<%@page import="javax.servlet.http.Cookie"%>

<html>

<%
	Cookie cookies[] = request.getCookies();
	Cookie nome = null;
	Cookie aux_login = null;
	int auxiliar = 0;
	Cookie idaux = null;
	String jog1="",jog2="";

	if (cookies != null) {
		for (int i = 0; i < cookies.length; i++) {
			out.println("<br>"+cookies[i].getName()+" <br>");
			if (cookies[i].getName().equals("username")) {
				nome = cookies[i];
			}
			if (cookies[i].getName().equals("aux_login")) {
				aux_login = cookies[i];
			}

			if (cookies[i].getName().equals("idsala")) {
				idaux = cookies[i];
			}

		}
	}
	out.println("ARE NULL ??");

	if (nome == null) {
		out.println("Cookie 'username' nao encontrada");
        else
         welcome nome.getValue()...



	}


So at the first load cookies are not found (that nome == null is verified), but if I refresh the page they are found - so they exist, but in a first load they are not being detected...

What am I doing wrong? Thinking about the matter this would be normal (the information displayed of course) if the page was the same, but it's a different page...
Posted
Updated 15-Apr-11 4:52am
v6

You should add the domain for that cookie using path. By default a cookie is only valid for that page only (in this case the login page). For more info (and have a look at path, you should add ;path=/):
http://javascript.about.com/library/blwcookie.htm[^]

For JSP have a look at setDomain:
http://www.tutorialspoint.com/jsp/jsp_cookies_handling.htm[^]

Good luck!
 
Share this answer
 
v2
I think i know what's happening, my login page is index.jsp and my "second page" is default.jsp (yes I know this is bad practice, but I'm just learning JSP for a few days, give me time :) ). So, I tried to do:

SQL
if (nome == null) {
    out.println("Cookie 'username' nao encontrada");
    and with js script..
    window.location.reload();



I was redirectedto index.jsp. Thats when I noticed when I do a sucessfull login (and go to default.jsp) my URL is still (for some motive!!) index.jsp.
Looks like its because doing the redirect this way:

XML
<jsp:forward page="/default.jsp" />
<%


Problem solved with response.sendRedirect("default.jsp");

Be advised that request.getParameter("t1") from previous page will not work anymore.

P.S: It's always safer use sessions for authentication.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900