wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet to jsp" Watch "servlet to jsp" New topic

servlet to jsp

adam braunton

Joined: Mar 22, 2002
Posts: 7
Hi, I need to pass an object from a servlet to a JSP.....er... how?
or another option would be to POST the data to the jsp from the servlet, is this possible?
Vin Man

Joined: Jun 13, 2001
Posts: 26
from your servlet, you would used the request's setAttribute("myObject", objectValue). Then on your jsp, you would include similar code as follows:
myObjectType myJspObject;
myJspObject = (myObjectType) request.getAttribute("myObject");
adam braunton

Joined: Mar 22, 2002
Posts: 7
...sounds like what i'm looking for, but what would the servlet code look like?
how does the servlet->jsp action kick-off?
adam braunton

Joined: Mar 22, 2002
Posts: 7
i'm also having trouble using the forward() method of RequestDispatcher, but that's another issue....
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
There are a couple of ways to do this depending on how long you want the object to last.
Like Vin suggested you can set an attribute in the request itself, it'll last as long as the request is good for, the syntax is:
// to set it
request.setAttribute("attributeName", atrObject);
request is the ServletRequest object passed in to your doXXX method .
You can use the Session object to store items too, the objects will last as long as the session is valid:
// to set
session.setAttribute("attributeName", atrObject);
the session variable is an HttpSession object you can get from the request.getSession() method.
You can use the ServletContext to store objects too, these object will last as long as the current context is valid.
// to set
cntx.setAttribute(("attributeName", atrObject);
cntx is a Servletcontext object you can get from calling getservletContext() in the servlet.
In the JSP you'd access request object usig the implicit request object and the servletcontext using the implicit application object. the session you'll have to get yourself using the request object.
hope all that helps you out...

I agree. Here's the link: http://aspose.com/file-tools
subject: servlet to jsp
Similar Threads
session variable
how to redirect from a single servlet to more than one jsp's?
JSP to Servlet call
Attributes sent from JSP wont show in Servlet
OutputStream irritates