Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I set a request scope variable in a servlet. and get in a jsp.

Status
Not open for further replies.

FreshmenProgrammer

Programmer
Jan 16, 2006
42
CA
Hello, Thank you for reading this post. I know how to set a session variable and get a session variable in a servlet. How do you set and get a request scope or the next one down from session. and how can I get it from a jsp page also. Thank you.

EG.

set
request.getSession().setAttribute("name", name);

to get it in a servlet
String name = (String)request.getSession().getAttribute("name");

to get it in a jsp
${seesionScope.name}
 
Hi,

You can set the request attribute in the servlet and can get that request attribute in JSP page as servlet is forwarding the same request to the JSP page. But the other way you i.e.., you cannot set the request attribute in the JSP and can get the request attribute in the Servlet as HTTP is a stateless protocol and for every request to the server it will create a new request and response object.

Ex: Possible
Code:
 // in Servlet
 request.setAttribute("msg","Hello World! From Servlet");

 // in the JSP
 out.println(request.getAttribute("msg")); // will print Hello World! From Servlet on the JSP page

Ex: Not Possible
Code:
 // In JSP page
 request.setAttribute("msg", "Hello World! From JSP");

 // In Servlet
 request.getAttribute("msg"); // will throw null pointer exception

Hope this will help you.

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top