[ Team LiB ] Previous Section Next Section

Built-in JSP Objects

JSPs create a simplified layer on top of the servlet API. Aside from making it easy to produce HTML output, the JavaServer Pages API makes it easy to access information provided by objects that represent a request, response, or session. You access these objects through implicit, or built-in, objects.

The built-in objects and their associated implementation classes are shown in Table 8.1.

Table 8.1. Built-in JSP Objects and Their Implementation Classes

JSP Object

Implementation Class

request

HttpServletRequest

response

HttpServletResponse

out

JspWriter

session

HttpSession

application

ServletContext

pageContext

PageContext

config

ServletConfig

page

Object

exception

Throwable

These objects are implicitly created at the beginning of a page when your JSP is invoked, and they directly reference objects in the underlying servlet. They are always available to scriptlets and expressions. As such, you use them the same way you would if you were writing a servlet. From earlier reading, you should already be familiar with the operation of many of these objects.

The request Object

The request object represents the current request from the browser and is a subclass of the ServletRequest class. For most current implementations of JSP, the request object is an instance of HttpServletRequest. If your JSP engine supports protocols other than HTTP, you might find that your request object is an instance of some other protocol-specific subclass of ServletRequest.

The response Object

Like the request object, the response object is usually a subclass of the HTTP-specific version of ServletResponse. In other words, it is usually an instance of HttpServletResponse. Again, if you have a JSP engine that is centered on a protocol other than HTTP, the response object might be an instance of a different class.

The out Object

The out object is responsible for writing responses back to the browser and is an instance of the JspWriter class. You will learn more about the JspWriter class later in this chapter.

The session Object

The session object is an instance of HttpSession. Because there is an obvious dependence on HTTP, this object is available only if your JSPs use HTTP.

The application Object

The application object is an instance of the ServletContext object. The names "application" and "ServletContext" don't seem very similar, but if you recall what the ServletContext class does, you'll see that it manages data at the application level.

The pageContext Object

The pageContext object is an instance of the PageContext class. Many of the items available through built-in variables are also available through the pageContext object. You will learn more about the pageContext object later in this chapter.

The config Object

The config object gives you access to configuration information for your JSP and is an instance of the ServletConfig class.

The page Object

The page object is rather peculiar because it is a reference to the current JSP. In other words, it's like an alias for the this keyword in Java. Although most JSPs are currently written in Java, the JSP architecture is designed to allow scripting in languages other than Java. These other languages also need to support the built-in objects, such as request, response, and out. In those languages, there might not be a this keyword, so the page object provides a reference to the current page instead.

The exception Object

When you create an error page to deal with exceptions that occur during normal JSP processing, you might need access to the exception that caused the error page to be invoked. You can access that exception through the exception object.

    [ Team LiB ] Previous Section Next Section