Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Implicit Objects

JSP Implicit Objects

JSP implicit objects are Java objects provided by the JSP container for each page; developers can use them directly without explicit declaration. JSP implicit objects are also called predefined variables.

The nine implicit objects supported by JSP:

object Description
request HttpServletRequest An instance of the interface
response HttpServletResponse An instance of the interface
out JspWriterAn instance of the class used to output results to the web page
session HttpSessionclass
application ServletContextAn instance of the class related to the application context
config ServletConfigclass
pageContext PageContextAn instance of the class that provides access to all objects and namespaces of the JSP page
page Similar to the this keyword in Java classes
Exception ExceptionAn object of the class representing the corresponding exception object in the JSP page where the error occurred

The request Object

The request object is an instance of the javax.servlet.http.HttpServletRequest class. Whenever a client requests a JSP page, the JSP engine creates a new request object to represent that request.

The request object provides a series of methods to obtain HTTP header information, cookies, HTTP methods, and so on.


The response Object

The response object is an instance of the javax.servlet.http.HttpServletResponse class. When the server creates the request object, it also creates the response object used to respond to that client.

The response object also defines interfaces for handling HTTP headers. Through this object, developers can add new cookies, timestamps, HTTP status codes, and so on.


The out Object

The out object is an instance of the javax.servlet.jsp.JspWriter class, used to write content into the response object.

The initial JspWriter object is instantiated differently depending on whether the page is buffered. You can easily turn off buffering by using the buffered='false' attribute in the page directive.

The JspWriter class contains most of the methods in the java.io.PrintWriter class. However, JspWriter adds some methods designed specifically for handling buffering. Also, the JspWriter class throws IOExceptions, while PrintWriter does not.

The following table lists important methods we will use to output data of types such as boolean, char, int, double, String, and object:

Method Description
out.print(dataType dt) Outputs a value of type Type
out.println(dataType dt) Outputs a value of type Type and then a new line
out.flush() Flushes the output stream

The session Object

The session object is an instance of the javax.servlet.http.HttpSession class. It behaves in the same way as the session object in Java Servlets.

The session object is used to track sessions across various client requests.


The application object

The application object directly wraps the servlet's ServletContext class object and is an instance of the javax.servlet.ServletContext class.

This object represents the JSP page throughout the entire lifecycle of the page. It is created when the JSP page is initialized and removed when the jspDestroy() method is called.

By adding attributes to the application, all JSP files that make up your web application can access these attributes.


The config object

The config object is an instance of the javax.servlet.ServletConfig class and directly wraps the servlet's ServletConfig class object.

This object allows developers to access initialization parameters of the servlet or JSP engine, such as file paths.

The following is how to use the config object; it is not very important, so it is not commonly used:



config.getServletName();

It returns the servlet name contained in the element. Note that theelement is defined in the WEB-INF\web.xml file.


The pageContext object

The pageContext object is an instance of the javax.servlet.jsp.PageContext class and is used to represent the entire JSP page.

This object is mainly used to access page information while filtering out most implementation details.

This object stores references to the request and response objects. The application, config, session, and out objects can be derived by accessing the attributes of this object.

The pageContext object also contains directive information passed to the JSP page, including caching information, ErrorPage URL, page scope, and so on.

The PageContext class defines several fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE. It also provides more than 40 methods, half of which are inherited from the javax.servlet.jsp.JspContext class.

One important method is removeAttribute(), which can take one or two arguments. For example, pageContext.removeAttribute("attrName") removes the relevant attribute from all four scopes, but the following form removes the relevant attribute only from a specific scope:



pageContext.removeAttribute("attrName", PAGE_SCOPE);


The page object

This object is a reference to the page instance. It can be regarded as a representative of the entire JSP page.

The page object is a synonym for the this object.


The exception object

The exception object wraps information about exceptions thrown from previous pages. It is typically used to generate an appropriate response to error conditions.