Implicit Objects

Implicit objects provide access to many servlet capabilities in the context of a JavaServer Page. Implicit objects have four scopes: application, page, request and session. The JSP container owns objects with application scope. Any JSP can manipulate such objects. Objects with page scope exist only in the page that defines them. Each page has its own instances of the page-scope implicit objects. Objects with request scope exist for the duration of the request. For example, a JSP can partially process a request, then forward it to a servlet or another JSP for further processing. Request-scope objects go out of scope when request processing completes with a response to the client. Objects with session scope exist for the client's entire browsing session. Figure 27.2 describes the JSP implicit objects and their scopes. This chapter demonstrates several of these objects.

Figure 27.2. JSP implicit objects.

(This item is displayed on page 1286 in the print version)

Implicit object

Description

Application Scope

 

application

A javax.servlet.ServletContext object that represents the container in which the JSP executes.

Page Scope

 

config

A javax.servlet.ServletConfig object that represents the JSP configuration options. As with servlets, configuration options can be specified in a Web application descriptor.

exception

A java.lang.Throwable object that represents an exception that is passed to a JSP error page. This object is available only in a JSP error page.

out

A javax.servlet.jsp.JspWriter object that writes text as part of the response to a request. This object is used implicitly with JSP expressions and actions that insert string content in a response.

page

An Object that represents the this reference for the current JSP instance.

pageContext

A javax.servlet.jsp.PageContext object that provides JSP programmers with access to the implicit objects discussed in this table.

response

An object that represents the response to the client and is normally an instance of a class that implements HttpServletResponse (package javax.servlet.http). If a protocol other than HTTP is used, this object is an instance of a class that implements javax.servlet.ServletResponse.

Request Scope

 

request

An object that represents the client request and is normally an instance of a class that implements HttpServletRequest (package javax.servlet.http). If a protocol other than HTTP is used, this object is an instance of a subclass of javax.servlet.Servlet-Request.

Session Scope

 

session

A javax.servlet.http.HttpSession object that represents the client session information if such a session has been created. This object is available only in pages that participate in a session.

Note that many of the implicit objects extend classes or implement interfaces discussed in Chapter 26. Thus, JSPs can use the same methods that servlets use to interact with such objects, as described in Chapter 26. Most of the examples in this chapter use one or more of the implicit objects in Fig. 27.2.

Категории