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 known as predefined variables.
The nine implicit objects supported by JSP:
| Object | Description |
|---|---|
| request | HttpServletRequestInstance of the interface |
| response | HttpServletResponseInstance of the interface |
| out | JspWriterInstance of the class, used to output results to the web page |
| session | HttpSessionInstance of the class |
| application | ServletContextInstance of the class, related to the application context |
| config | ServletConfigInstance of the class |
| pageContext | PageContextInstance of the class, provides access to all objects and namespaces of the JSP page |
| page | Similar to the this keyword in Java classes |
| Exception | ExceptionObject of the class, representing the corresponding exception object in the JSP page where the error occurred |
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 this request.
The request object provides a series of methods to obtain HTTP header information, cookies, HTTP methods, and so on.
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 a response object to respond to this client.
The response object also defines interfaces for handling HTTP header modules. Through this object, developers can add new cookies, timestamps, HTTP status codes, and so on.
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 class object is instantiated differently depending on whether the page has caching. You can easily turn off caching 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 specifically designed for handling caching. Also, the JspWriter class throws IOExceptions, while PrintWriter does not.
The following table lists the important methods we will use to output data of types such as boolean, char, int, double, String, object, etc.:
| Method | Description |
|---|---|
| out.print(dataType dt) | Outputs a value of Type |
| out.println(dataType dt) | Outputs a value of Type and then a newline |
| out.flush() | Flushes the output stream |
session object
The session object is an instance of the javax.servlet.http.HttpSession class. It behaves the same as the session object in Java Servlets.
The session object is used to track sessions between various client requests.
application object
The application object directly wraps the servlet's ServletContext object and is an instance of the javax.servlet.ServletContext class.
This object represents this JSP page throughout its entire lifecycle. This object is created when the JSP page is initialized and is 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.
config object
The config object is an instance of the javax.servlet.ServletConfig class, directly wrapping 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 are usage methods of the config object. They are not very important, so they are not commonly used:
config.getServletName();
It returns the servlet name contained in the <servlet-name> element. Note that the <servlet-name> element is defined in the WEB-INF\web.xml file.
pageContext object
The pageContext object is an instance of the javax.servlet.jsp.PageContext class, 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 object and the response object. The application object, config object, session object, and out object can be exported by accessing the properties of this object.
The pageContext object also contains instruction information passed to the JSP page, including caching information, ErrorPage URL, page scope, etc.
The PageContext class defines some 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 of the important methods is removeAttribute(), which can accept one or two parameters. For example, pageContext.removeAttribute("attrName") removes the relevant attribute from all four scopes, but the following method only removes the relevant attribute from a specific scope:
pageContext.removeAttribute("attrName", PAGE_SCOPE);
page object
This object is a reference to the page instance. It can be regarded as a representation of the entire JSP page.
The page object is a synonym for the this object.
exception object
The exception object wraps the exception information thrown from a previous page. It is typically used to generate an appropriate response to error conditions.
Other Extensions