File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Passing attributes from servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing attributes from servlet to jsp" Watch "Passing attributes from servlet to jsp" New topic
Author

Passing attributes from servlet to jsp

sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
Hi
I have a situation where from the first jsp i am invoking servlets do post method with a ajax and getting some text data to first jsp, at the same time i want to make some objects available to 1 st as well as another (2nd ) jsp page.what i am thinking is to set this attribute in session scope and then i get them in any jsp i want but i want to whether its is possible to do this and if possible then can you show me with little code example.I am new to web technology.
Thanks
Sruthi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Without knowing what the relationship between the first and second JSP pages is, impossible to say anything.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
second jsp page will be called from first jsp with href.there will links in the first jsp and with click on link second jsp will open.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

OK, so this is no different than any other scenario where data from one page needs to made available to another. Either the data is included as part of the submission data, or is stored as scoped variables in the session. Pick whichever one makes the most sense.
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
ya this is pretty much same senario but the thing is i want data from servlet to use in second jsp, not from first jsp to second jsp. so this still work for this situation and also when i set attributes in session scope, do i need to forward page using request dispatcher. as i already told, i am using ajax to call do post of servlet and i am getting data to first jsp as ajax response. so right now i am not forwarding to any page as it works fine for me. give me your suggestion as how to approach
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

The session sounds like the best bet for your scenario. be sure to clean up the scoped variables when they are no longer needed.
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
I used session as well as context attributes to test my situation and i am getting null values when i am using for the first request and then for sebsequent requests , i am getting my actual values. so can suggest me something so that i am able to get values for the first request.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

If nothing has set values into the session for the first request, why should you expect anything to be there?
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
ya i understand the situation. anyway i was testing this in my first jsp for the frist request, its going to be null.So my question is after making the first request from my first jsp, i can use set values in my second jsp if user clicks the link in my first jsp.is it possible or still the values wont be set.
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
Thanks Bear Bibeault
I think my problem is solved for now.Thanks for your suggestions
 
 
subject: Passing attributes from servlet to jsp