aspose file tools*
The moose likes Struts and the fly likes How to use httprequest object through out the application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to use httprequest object through out the application" Watch "How to use httprequest object through out the application" New topic
Author

How to use httprequest object through out the application

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

hi i am working with struts 1.3 from one of my action I am setting some values into my request object e.g. the name of the user. Now I want to use the same request throughout my application and I can not pass this request to other method because till I used in other part the values from the request lost.I know about ServletConfig and ServletSontext can i use it here or is there any other option available plus when I created a new HttpRequest object on different page does it affect the old request...??

please sort out the above doubts if you get it or asked me to clear it more.....


kaustubh


No Kaustubh No Fun, Know Kaustubh Know Fun..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Data that needs to be carried around the app for a specific user should be put in the *session*. The request is recreated for every request, as the name implies.
Chankya Sharma
Greenhorn

Joined: May 20, 2010
Posts: 1
I agree with David, if you want to keep the data accessible every where for a user than you need to put in a Session. You can also do this through the form files and set the scope of the file to Session in struts-config. Than you just need to get the form file and fetch the data from it where ever you want.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

An action form isn't an appropriate place for non-Struts-specific information like user data; that should just be stored in the session by itself (or as part of an application-specific session data object).
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

I know about the session sir but and when I set everything into it I have to pass it into method I wanted to use it.But I need a solution from where I can use this values through out my application without passing it to the method because I have to change lot of places where the method is used........

kaustubh
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If it's in the session you don't have to pass it anywhere--it's available anywhere the session is.

If you're saying you want to abstract it out of the session then create a composite object containing the session objects you want to pass and pass that into your methods; if the data contained in the composite object changes you'd have to change your methods anyway.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

You want to say that once I set all the values in the request whereever I create the HttpRequest's object from there I can extract the same values???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
No. You shouldn't place any of those objects into the request - they should go into the user's session.

Please restrict yourself to a single question mark at the end of a question; it's sufficient.


Ping & DNS - my free Android networking tools app
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

So in case If i create a new object of HttpServletRequest in other class did I get the same values which I set in different class?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Of course not--you'll have just created a new request object. But you're still talking about request, while we're talking about session--so something's getting misunderstood.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Sorry It was a mistake I was talking about HttpSession object will it be work if i create a new one on other side of the application...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You don't *need* to create a new one (and if you did, it would be just that--a new session. You want the *existing* session.)
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Then everytime I have to pass this session object everywhere in my application. What if I want to do it as a centralize object where it can be used by every servlet and jsp?

kaustubh
Glen Divers
Ranch Hand

Joined: Jan 21, 2010
Posts: 61
Kaustubh G Sharma wrote:Then everytime I have to pass this session object everywhere in my application.


Struts handles this for you - all you have to do is access the session object where you need it.

Kaustubh G Sharma wrote:
What if I want to do it as a centralize object where it can be used by every servlet and jsp?

kaustubh


If you have a "global" object, store it in the Application Context.

Regards
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

gotcha thank you guys for your help and support.....

kaustubh
 
Consider Paul's rocket mass heater.
 
subject: How to use httprequest object through out the application