File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Retrieving Cookies After actionMapping.findForward( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Retrieving Cookies After actionMapping.findForward("")    " Watch "Retrieving Cookies After actionMapping.findForward("")    " New topic
Author

Retrieving Cookies After actionMapping.findForward("")

Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Hi All

In my application i am doing search and putting search criteria into the cookies in my struts action class. This class set this values into the cookie and then forward with





The "searchsucccess" leads us to a jsp page.
Now i want the jsp page to retrieve those cookies values in it which i have set in action class like:




But these are coming as null. Like cookies has not been created anywher.

Anybody has any idea.
Regards.


Khushwinder
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You will not be able to read the cookies until a round trip to and from the client has been made.

A forward takes place, entirely, on the server without involving the browser so the cookies that you set won't actually exist until the browser receives the response and stores them. When the browser makes it's next request, it will include the cookies in the request headers and you will be able to read them by calling request.getCookies.

If you want to work with those variables from within the same request/response cycle, you might want to also bind them to request scope with request.setAttribute. You will then have access to them from all the components in your request/response chain.
[ February 14, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Hi Ben
If i use redirect=true can it work or not?
I mean any way to send any response to browser and still having cookies?
Regards.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Why are you using cookies to transfer the data? That's really really odd.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Bear's question is valid and should be answered.

To answer yours, yes, sendRedirect involves a round trip to the browser and an entirely new request.
Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Hi Ben,Bear

You people are right that is the most awkward way of doing the things.
But i have been ordered to that as i am not the senior most person in my company and i have to obey the things.

Now coming to my question:

in my struts-config.xml file i am using redirect="true" like:

>forward name="searchsuccess" path="/admin/showStones.do?dispatch=search" redirect="true" />

but still no the next page i am not able to retrieve the cookies values.
Is there anything else i need to do in my case.
Regards.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Moving to our Struts forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving Cookies After actionMapping.findForward("")