setting cookies in servlet and then forwarding does not work

R

rp

Please tell me i can do this:
1)i go to a jsp page and if it does not find the exist of a cookie it
forwards to a login screen

2) the login screen submits to a servlet, and then set some cookies
and then forwards to the original JSP

3) the orig jsp sees those cookies and life is fine.

well i can not make the servlet set the cookies so that the forwarding
jsp sees those cookies.

====
Cookie cookie1 = new Cookie("USERID",_User.getUname());
cookie1.setMaxAge(-1);
cookie1.setPath("/");
res.addCookie(cookie1);

Cookie cookie2 = new Cookie("USERDIR",_User.getDir());
cookie2.setMaxAge(-1);
cookie2.setPath("/");
res.addCookie(cookie2);

RequestDispatcher disp =
getServletConfig().getServletContext().getRequestDispatcher("/my.jsp");
disp.include(req,res);


my.jsp does not see the cookies.

if i just refresh the browser the second time in the cookies are there

What the heck am i doing wrong
thanks
 
P

Paul Roberts

Please tell me i can do this:
1)i go to a jsp page and if it does not find the exist of a cookie it
forwards to a login screen
2) the login screen submits to a servlet, and then set some cookies
and then forwards to the original JSP
3) the orig jsp sees those cookies and life is fine.
well i can not make the servlet set the cookies so that the forwarding
jsp sees those cookies.
====
Cookie cookie1 = new Cookie("USERID",_User.getUname());
cookie1.setMaxAge(-1);
cookie1.setPath("/");
res.addCookie(cookie1);
Cookie cookie2 = new Cookie("USERDIR",_User.getDir());
cookie2.setMaxAge(-1);
cookie2.setPath("/");
res.addCookie(cookie2);
RequestDispatcher disp =
getServletConfig().getServletContext().getRequestDispatcher("/my.jsp");
disp.include(req,res);

my.jsp does not see the cookies.
if i just refresh the browser the second time in the cookies are there
What the heck am i doing wrong thanks

The cookies won't be read back until the next reload of the page.
 
J

John C. Bollinger

rp said:
Please tell me i can do this:
1)i go to a jsp page and if it does not find the exist of a cookie it
forwards to a login screen

2) the login screen submits to a servlet, and then set some cookies
and then forwards to the original JSP

3) the orig jsp sees those cookies and life is fine.

well i can not make the servlet set the cookies so that the forwarding
jsp sees those cookies.

====
Cookie cookie1 = new Cookie("USERID",_User.getUname());
cookie1.setMaxAge(-1);
cookie1.setPath("/");
res.addCookie(cookie1);

Cookie cookie2 = new Cookie("USERDIR",_User.getDir());
cookie2.setMaxAge(-1);
cookie2.setPath("/");
res.addCookie(cookie2);

RequestDispatcher disp =
getServletConfig().getServletContext().getRequestDispatcher("/my.jsp");
disp.include(req,res);


my.jsp does not see the cookies.

if i just refresh the browser the second time in the cookies are there

What the heck am i doing wrong

You are adding cookies to the response. You are reading cookies from
the request. These are different objects.

You might solve this problem by redirecting back to the original JSP
instead of forwarding to it. If you use cookies for this then you will
need an HTTP round trip in there somehow. An alternative would be to
store the relevant user information in an object in the session, instead
of in cookies. Presence of the object would indicate that the user was
logged in. Or you could put an object in every session that has a
property indicating whether or not the user has authenticated.

The session-based solutions have the added advantages of not exposing
the user information to possible interception (except possibly during
login) and of working even if the user has cookies turned off (provided
that you are careful with your URL handling so as to enable potential
URL rewriting).

John Bollinger
(e-mail address removed)
 
W

Wendy S

rp said:
if i just refresh the browser the second time in the cookies are there
What the heck am i doing wrong

The cookies aren't there until the next request. Maybe use redirect instead
of forward, to cause another round trip to the server?
 
S

Sudsy

rp said:
Please tell me i can do this:
1)i go to a jsp page and if it does not find the exist of a cookie it
forwards to a login screen

2) the login screen submits to a servlet, and then set some cookies
and then forwards to the original JSP

3) the orig jsp sees those cookies and life is fine.

well i can not make the servlet set the cookies so that the forwarding
jsp sees those cookies.
What the heck am i doing wrong
thanks

Do a redirect instead of a forward.
By forcing a redirect to the original page, the cookies get sent
back to the browser which then sends them along with the request
for the page to which it is redirected.
You almost need a diagram to show the flow but be assured that a
lot of people get caught on this one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top