JSP implicit objects are Java objects provided by the JSP container for each page, which developers can use directly without explicit declaration. JSP implicit objects are also known as predefined variables.
The nine implicit objects supported by JSP are:
| Object | Description |
|---|---|
| request | An instance of the HttpServletRequest interface |
| response | An instance of the HttpServletResponse interface |
| out | An instance of the JspWriter class, used to output results to the web page |
| session | An instance of the HttpSession class |
| application | An instance of the ServletContext class, related to the application context |
| config | An instance of the ServletConfig class |
| pageContext | An instance of the PageContext class, providing access to all objects and namespaces of the JSP page |
| page | Similar to the this keyword in a Java class |
| Exception | An object of the Exception class, representing the exception object corresponding to the JSP page where an 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 that request.
The request object provides a series of methods to obtain HTTP header information, cookies, HTTP methods, etc.
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 that client.
The response object also defines an interface for handling the HTTP header module. Through this object, developers can add new cookies, timestamps, HTTP status codes, etc.
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 a buffer. You can easily disable buffering by using the buffered='false' attribute in the page directive.
The JspWriter class contains most of the methods from the java.io.PrintWriter class. However, JspWriter adds some methods specifically designed for handling buffering. Additionally, the JspWriter class throws IOExceptions, while PrintWriter does not.
The following table lists the important methods we will use to output data 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 adds 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 across different client requests.
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 its entire lifecycle. 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.
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 the initialization parameters of the Servlet or JSP engine, such as file paths, etc.
Here 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 <servlet-name> element. Note that the <servlet-name> element is defined in the WEB-INFweb.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 primarily 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 derived by accessing the properties of this object.
The pageContext object also contains directive information passed to the JSP page, including caching information, ErrorPage URL, page scope, etc.
The PageContext class defines several fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE. It also provides over 40 methods, half of which are inherited from the javax.servlet.jsp.JspContext class.
One important method is removeAttribute(), which can accept one or two parameters. For example, pageContext.removeAttribute("attrName") removes the attribute from all four scopes, but the following method only removes the attribute from a specific scope:
pageContext.removeAttribute("attrName", PAGE_SCOPE);
page Object
This object is a reference to the page instance. It can be considered as the representative of the entire JSP page.
The page object is synonymous with 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.
YouTip