*
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


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

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
Sheriff

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
Author
Ranch Hand

Joined: May 07, 2001
Posts: 57
If you are interested in a complete example of the above code check out this FAQ.
http://www.jspinsider.com/faq/12.view
Jayson Falkner
jayson@jspinsider.com


Jayson Falkner<br />jayson@jspinsider.com<br />Author of <a href="http://www.jspbook.com" target="_blank" rel="nofollow">Servlets and JavaServer Pages; the J2EE Web Tier</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: grabbing a parameter in jsp
 
Similar Threads
config.getInitParameter does'nt work
IllegalStateException RequestDispatcher.forward
include file question!
saving an input element state(value)
IllegalStateException error