JSP Implicit Objects
JSP implicit objects are Java objects provided by the JSP container for each page, which developers can use directly without explicit declaration. These objects are also known as predefined variables.
The nine implicit objects supported by JSP are:
Object | Description |
---|---|
request | Instance of HttpServletRequest interface |
response | Instance of HttpServletResponse interface |
out | Instance of JspWriter class, used to output results to the web page |
session | Instance of HttpSession class |
application | Instance of ServletContext class, related to the application context |
config | Instance of ServletConfig class |
pageContext | Instance of PageContext class, provides access to all objects and namespaces of the JSP page |
page | Similar to the 'this' keyword in Java classes |
Exception | Object of Exception class, representing the exception object corresponding to the error JSP page |
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 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 a request object, it also creates a response object to respond to the client.
The response object also defines an interface for handling HTTP header modules. 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 to the response object.
The initial JspWriter class object is instantiated differently based on whether the page is buffered. Buffering can be easily turned off by using the buffered='false' attribute in the page directive.
The JspWriter class includes most methods from the java.io.PrintWriter class. However, JspWriter adds some methods designed specifically for handling buffering. Also, the JspWriter class throws IOExceptions, whereas PrintWriter does not.
The following table lists important methods used to output boolean, char, int, double, String, object, etc.:
Method | Description |
---|---|
out.print(dataType dt) | Outputs the value of Type |
out.println(dataType dt) | Outputs the value of Type and then换行 |
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 the session across client requests.
application Object
The application object directly wraps the servlet's ServletContext class object, 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 initialization parameters of the Servlet or JSP engine, such as file paths.
The following is a usage method of the config object, which is not very important and thus not commonly used:
config.getServletName();
It returns the servlet name contained in the <servlet-name> element, which 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 primarily used to access page information while filtering out most implementation details. This object holds references to the request and response objects. The application, config, session, and out objects can be accessed through the properties of this object.
The pageContext object also contains directive information passed to the JSP page, including cache information, ErrorPage URL, page scope, etc.
The PageContext class defines several fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, 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 related attribute from all four scopes, but the following method only removes the related attribute from a specific scope:
pageContext.removeAttribute("attrName", PAGE_SCOPE);
page object
This object is a reference to the page instance. It can be seen 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.