JSP – Implicit Objects

JSP – Implicit Objects 2017-06-03T23:18:41+00:00

The available predefined variables or implicit objects are request, response, out, session, application, config, pageContext, and page.

These objects determine how can we accept the request from the browser, send the response from the server and also determine how session tracking can be done.

1 request :

This is the HttpServletRequest associated with the request, and lets you look at the request parameters (via getParameter), the request type (GET, POST, HEAD, etc.), and the incoming HTTP headers (cookies, Referer, etc.).

Strictly speaking, request is allowed to be a subclass of ServletRequest other than HttpServletRequest, if the protocol in the request is something other than HTTP.

This is almost never done in practice.

2 response :

This is the HttpServletResponse associated with the response to the client.

Note that, since the output stream (see out below) is buffered, it is legal to set HTTP status codes and response headers, even though this is not permitted in regular servlets once any output has been sent to the client.

3 out :

This is the PrintWriter used to send output to the client.

However, in order to make the response object (see the previous section) useful, this is a buffered version of PrintWriter called JspWriter.

Note that you can adjust the buffer size, or even turn buffering off, through use of the buffer attribute of the page directive.

Also note that out is used almost exclusively in scriptlets, since JSP expressions automatically get placed in the output stream, and thus rarely need to refer to out explicitly.

4 session :

This is the HttpSession object associated with the request.

Recall that sessions are created automatically, so this variable is bound even if there was no incoming session reference.

The one exception is if you use the session attribute of the page directive to turn sessions off, in which case attempts to reference the session variable cause errors at the time the JSP page is translated into a servlet.

5 application :

This is the ServletContext as obtained via getServletConfig(). getContext(). It is used to share information among all users of a currently active application.

6 config :

This is the ServletConfig object for this page. This stores servlet configuration data.

7 pageContext :

JSP introduced a new class called PageContext to encapsulate use of server-specific features like higher performance JspWriters.

The idea is that, if you access them through this class rather than directly, your code will still run on “regular” servlet/JSP engines.

8 page :

This is simply a synonym for this, and is not very useful in Java.

It was created as a placeholder for the time when the scripting language could be something other than Java.

9 exception :

This implicit object exists only in a defined error page. It holds a reference to the uncaught exception that caused the error page to be called.

Prev Next