File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes share data from servlet back to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "share data from servlet back to jsp" Watch "share data from servlet back to jsp" New topic

share data from servlet back to jsp

medswa chan

Joined: Jul 15, 2011
Posts: 1
Hello, i wanted to know what is the best practice to share data between the servlet and jsp. in my project which was wriiten years back they use a lot of usebean in session, when it is not required in session scope. url rewriting doesnt always help as we cant keep passing stuff everywhere. i need a bean to be available in a series of back and forth calls between jsp and servlet , and i put it in the session scope, the first time it works but the second time when it hits the servlet where i update the session again and come bac to the same jsp, the session is not getting updated (i use tomcat). i tried manually removing it from session and putting it back again, but doesnt work. so i am wondering if there is some other way or should i use request scope for the bean. please help.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If the session is forwarding to the JSP, then putting data into the request scope is sufficient.

If you are talking about preserving data from one request to the next, then you will have to put data into the session scope to preserve it.

Those are the "best practices" summarized in two sentences.

However you will have to look at your system and identify the request life-cycle. As it is we just have some sloppy talk about passing data from servlets to JSPs without any information about the requests involved. When I see you saying that a servlet "calls" a JSP, or vice versa, it's pretty clear that you aren't looking at the system properly.
I agree. Here's the link:
subject: share data from servlet back to jsp
It's not a secret anymore!