Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass values from servlet to jsp  RSS feed

 
Lakshmi siri
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How do we pass values from servlet to jsp?
Thanks
--Lakshmi

[ May 20, 2004: Message edited by: Lakshmi siri ]
[ May 20, 2004: Message edited by: Lakshmi siri ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want people to take the time to answer your questions, how about spending a few seconds of your own time to come up with meaningful topic titles? "Help me" doesn't tell anyone what your question is about; be they people who can answer the question for you, or people who might be looking for the same answer.

Hint: you can edit your original post to change the topic title.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36440
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshmi,
You can put the values in the request using request.setAttribute().
 
Gert Cuppens
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides request, you can also use session.
In the JSP you can define the scope of a java bean. This scope may be "page" (default value), "request", "session", "application".
I always use request and session.
What's the difference between request and session ?
A request can be compared with going to a bar, asking for a beer and paying it when you get it. The bartender doesn't have to keep in mind that you have to pay.
A session is going to a restaurant and eating � la carte. You demand a salad for starters, a main dish, a dessert and a coffee. And after that, you pay. The session starts with you demanding the salad and ends with you paying the bill.

SO you have a choice. If you can forget about the values as soon as you've showed the values of the java bean on a HTML page, use request. If you want to keep in mind what the user did during his visit on your web site, use session.

When using request, you should write
request.setAttribute("mybean", mybean)
And in the JSP you have
<jsp:useBean id="mybean" class="org.gertcuppens.general.MyBean" scope="request"/>

when using session you should write
HttpSession session = request.getSession(true); // if you didn't have a session in the request
or
HttpSession session = request.getSession(false) ; // if you already have put a session in the request and merely wants to add something to it

session.setAttribute("myBean", myBean);

And the JSP can read this value by
<jsp:useBean id="myBean" class="org.gertcuppens.general.MyBean" scope="session"/>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!