16.2 Implicit JSP Scripting Objects
Scripting elements can use predefined variables
that the container assigns as references to
implicit objects (Table 16-1) to access request and application data. These
objects are instances of classes defined by the servlet and JSP
specifications. Appendix D contains complete descriptions of all
methods for each class, and they are briefly introduced here and used
in a number of examples in this chapter.
Table 16-1. Implicit JSP objects|
application
|
javax.servlet.ServletContext
|
config
|
javax.servlet.ServletConfig
|
exception
|
java.lang.Throwable
|
out
|
javax.servlet.jsp.JspWriter
|
page
|
java.lang.Object
|
pageContext
|
javax.servlet.jsp.PageContext
|
request
|
javax.servlet.http.HttpServletRequest
|
response
|
javax.servlet.http.HttpServletResponse
|
session
|
javax.servlet.http.HttpSession
|
These objects provide access to the same information (and more) as
the implicit variables you can use in EL expressions, but
it's not a one-to-one match:
- pageContext
-
The pageContext
variable contains a reference to an instance of the class named
javax.servlet.jsp.PageContext. It provides methods
for accessing references to all the other objects and attributes for
holding data that is shared between components in the same page.
It's the same object that you can access with the
${pageContext} EL expression. Attribute values for
this object represent the page scope; they are the same objects as
are available to the EL world as a Map represented
by the ${pageScope} expression.
- request
-
The request
variable contains a reference to an instance of a class that
implements an interface named
javax.servlet.http.HttpServletRequest. It provides
methods for accessing all the information that's
available about the current request, such as request parameters,
attributes, headers, and cookies. It's the same
object that you can access with the
${pageContext.request} EL expression. Attribute
values for this object represent the request scope; they are the same
objects as are available to the EL world as a Map
represented by the ${requestScope} expression.
- response
-
The response
variable contains a reference to an object representing the current
response message. It's an instance of a class that
implements the
javax.servlet.http.HttpServletResponse interface,
with methods for setting headers and the status code, and adding
cookies. It also provides methods related to session tracking. These
methods are the response methods
you're most likely to use. The same object can be
accessed with the ${pageContext.response} EL
expression.
- session
-
The session
variable allows you to access the client's session
data, managed by the server. It's assigned a
reference to an instance of a class that implements the
javax.servlet.http.HttpSession interface, which
provides access to session data as well as information about the
session, such as when it was created and when a request for the
session was last received. It's the same object that
you can access with the ${pageContext.session} EL
expression. Attribute values for this object represent the session
scope; they are the same objects as are available to the EL world as
a Map represented by the
${sessionScope} expression.
- application
-
The application
variable contains a reference to the instance of a class that
implements the javax.servlet.ServletContext
interface that represents the application. This object holds
references to other objects that more than one user may require
access to, such as a database connection pool shared by all
application users. It also contains log( ) methods
you can use to write messages to the container's log
file. It's the same object that you can access with
the ${pageContext.servletContext} EL expression.
Attribute values for this object represent the application scope;
they are the same objects as are available to the EL world as a
Map represented by the
${applicationScope} expression.
- out
-
The out
object is an instance of
javax.servlet.jsp.JspWriter. You can use the
print( ) and println( ) methods
provided by this object to add text to the response message body. In
most cases, however, you will just use template text and JSP action
elements instead of explicitly printing to the out
object.
- exception
-
The exception
object is available only in error pages and contains information
about a runtime error. It's the same object that you
can access with the ${pageContext.exception} EL
expression.
The remaining two implicit objects (config and
page) are so rarely used in scripting elements
that I don't discuss them here. If
you're interested, you can read about them in
Appendix D.
All variable names listed in Table 16-1 are
reserved for the implicit object references. If you declare your own
variables in a JSP page, you must not use these reserved variable
names.
|