Meaningless Drivel is fun!*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet to jsp" Watch "servlet to jsp" New topic
Author

servlet to jsp

adam braunton
Greenhorn

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?
thanks,
Vin Man
Greenhorn

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
Greenhorn

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
Greenhorn

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
Adam
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...


Dave
 
wood burning stoves
 
subject: servlet to jsp
 
Similar Threads
how to redirect from a single servlet to more than one jsp's?
OutputStream irritates
JSP to Servlet call
Attributes sent from JSP wont show in Servlet
session variable