| < Free Open Study > |
|
In the service methods of servlets we have references to many server-side objects that represent the incoming request, response, and session. JSP provides a set of implicit objects that can be used to access such objects. These objects are actually defined in the _JSP pageservice() method of the page implementation class and initialized with appropriate references.
JSP defines four scopes for the objects that can be used by the JSP authors:
Scope | Description |
---|---|
page | Objects can be accessed only within the JSP page in which they are referenced. |
request | Objects can be accessed within all the pages that serve the current request. These include pages that are forwarded to, and included in, the original JSP page to which the request was routed. |
session | Objects can only be accessed within the JSP pages accessed within the session for which the objects are defined. |
application | Application scope objects can be accessed by all JSP pages in a given context. |
The following table lists the implicit objects that can be used in scriptlets:
Implicit Object | Type | Scope | Description |
---|---|---|---|
request | Protocol dependent sub type of javax.servlet.ServletRequest | request | A reference to the current request |
response | Protocol dependent sub type of javax.servlet.ServletResponse | page | The response to the request |
pageContext | javax.servlet.jsp.PageContext | page | Provides a common point to access the request, response, session, and application, associated with the page being served |
session | javax.servlet.http.HttpSession | session | The session associated with the current request |
application | javax.servlet.ServletContext | application | The servlet context to which the page belongs |
out | javax.servlet.jsp.JspWriter | page | The object that writes to the response output stream |
config | javax.servlet.ServletConfig | page | The servlet configuration for the current page |
Page | java.lang.Object | page | An instance of the page implementation class that is serving the request (synonymous with the this keyword if Java is used as the scripting language) |
exception | java.lang.Throwable | page | Available with JSP pages that act as error pages for other JSP pages |
The following JSP page illustrates the use of implicit objects in JSP pages. Save this as %CATALINA_HOME%/webapps/jsp/Implicit.jsp:
<html> <head> <title>Implicit Objects</title> </head> <body style="font-family:verdana;font-size:10pt"> <p> Using Request parameters...<br>
We print the request parameter name:
Using Request parameters...<br> Name: <%= request.getParameter("name") %> </p>
We print a sentence using the out implicit variable:
<p> <% out.println("This is printed using the out implicit variable"); %> </p>
We store and retrieve an object of type String using the session implicit variable:
<p> Storing a string to the session...<br> <% session.setAttribute("name", "Meeraj"); %> Retrieving the string from session...<br> <b>Name:</b> <%= session.getAttribute("name") %> </p>
Store and retrieve an object of type String in the servlet context using the application implicit variable:
<p> Storing a string to the application...<br> <% application.setAttribute("name", "Meeraj"); %> Retrieving the string from application...<br> <b>Name:</b> <%= application.getAttribute("name") %> </p>
Finally, store and retrieve an object of type String in the page context using the pageContext implicit variable:
<p> Storing a string to the page context...<br> <% pageContext.setAttribute("name", "Meeraj"); %> Retrieving the string from page context...</br> <b>Name:</b> <%= pageContext.getAttribute("name") %> </p> </body> </html>
Access this JSP page at http://localhost:8080/jsp/Implicit.jsp?name=Meeraj and you will see:
| < Free Open Study > |
|