JSP Implicit Objects
JSP implicit objects are Java objects that the JSP container provides for each page, and developers can use them directly without explicitly declaring them. JSP implicit objects are also known as predefined variables.
Nine hidden objects supported by JSP:
|Request||Instance of the HttpServletRequest interface|
|Response||Instance of the HttpServletResponse interface|
|Out||An instance of the JspWriter class that outputs the results to a web page|
|Session||Instance of the HttpSession class|
|Application||An instance of the ServletContext class, related to the application context|
|Config||Instance of ServletConfig class|
|pageContext||An instance of the PageContext class that provides access to all objects and namespaces on the JSP page|
|Page||Similar to the this keyword in Java classes|
|Exception||An object of the Exception class representing the corresponding exception object in the JSP page where the error occurred|
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 the request.
The request object provides a set of methods to get HTTP header information, cookies, HTTP methods, and more.
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 that responds to this client.
The response object also defines the interface that handles the HTTP header module. With this object, developers can add new cookies, timestamps, HTTP status codes, and more.
The out object is an instance of the javax.servlet.jsp.JspWriter class that is used to write content in the response object.
The original JspWriter class object performs different instantiation operations depending on whether the page has a cache. 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 new methods designed to handle caching. Also, the JspWriter class throws an IOExceptions exception, and PrintWriter does not.
The following table lists the important methods we will use to output boolean, char, int, double, String, object and other types of data:
|out.print(dataType dt)||Output the value of type Type|
|out.println(dataType dt)||Output a value of type Type and then wrap it|
|out.flush()||Refresh the output stream|
The session object is an instance of the javax.servlet.http.HttpSession class. It has the same behavior as the session object in Java Servlets.
The session object is used to track sessions between individual client requests.
The application object directly wraps the servlet's ServletContext class object, which is an instance of the javax.servlet.ServletContext class.
This object represents this JSP page throughout the life of the JSP page. This object is created when the JSP page is initialized and removed as the jspDestroy() method is called.
By adding attributes to the application, all of the JSP files that make up your web application can access these properties.
The config object is an instance of the javax.servlet.ServletConfig class that wraps directly the object of the servlet's ServletConfig class.
This object allows developers to access initialization parameters of the servlet or JSP engine, such as file paths.
The following is the use of the config object, not very important, so it is not commonly used:
It's back included in<servlet-name>The servlet name in the element，note，<servlet-name>The elements are defined in the WEB-INF\web.xml file.
The pageContext object is an instance of the javax.servlet.jsp.PageContext class that represents the entire JSP page.
This object is mainly used to access page information, while filtering out most of the implementation details.
This object stores a reference 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 directive information passed to the JSP page, including cache information, ErrorPage URL, page scope, and so on.
The PageContext class defines fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, APPLICATION_SCOPE. It also provides more than 40 methods, half of which inherit from the javax.servlet.jsp.JspContext class.
One of the important methods is removeArribute(), which accepts one or two arguments. For example, pageContext.removeArribute("attrName") removes the related properties from the four scopes, but the following method only removes related properties from the specific scope:
This object is a reference to the page instance. It can be seen as a representative of the entire JSP page.
The page object is a synonym for this object.
The exception object wraps the exception information thrown from the previous page. It is usually used to generate an appropriate response to an error condition.