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

grabbing a parameter in jsp

Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
I am developing an application where I am using a servlet as a controller. From the servlet I forward to a jsp page. But I would like to pass a few variables from the servlet to the jsp. In the servlet, I appended the variables to the request by doing the following: request.setAttribute("var1", var1) then use the dispatcher to forward the page to the jsp.
But how can I access the variables (i.e. var1) in the JSP?
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Simple, Lets say you set a String in the servlet, in your jsp you place the code:
String fromServlet = (String)getAttribute("keyNameUsedInServlet);
You can set any object in the attributes and as long as you use of reference of the right type and cast it properly, it is now available in your JSP page.
Hope This Helps
Carl Trusiak, SCJP2

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Jayson Falkner
Ranch Hand

Joined: May 07, 2001
Posts: 57
If you are interested in a complete example of the above code check out this FAQ.
Jayson Falkner

Jayson Falkner<br /><br />Author of <a href="" target="_blank" rel="nofollow">Servlets and JavaServer Pages; the J2EE Web Tier</a>
I agree. Here's the link:
subject: grabbing a parameter in jsp
It's not a secret anymore!