English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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:
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 | HttpSessionAn instance of the class |
application | ServletContextAn instance of the class, related to the application context |
config | ServletConfigAn instance of the class |
pageContext | PageContextAn instance of the class, providing access to all objects and namespaces in the JSP page |
page | Similar to the this keyword in Java classes |
Exception | ExceptionThe object of the class, representing the exception object corresponding to the error in the JSP page |
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, etc.
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 this client simultaneously.
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.
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 performs different instantiation operations based on whether the page has a cache. It is easy to turn off caching by using the buffered='false' attribute in the page directive.
The JspWriter class includes most of the methods from the java.io.PrintWriter class. However, JspWriter also adds some methods specifically designed for handling caching. Additionally, the JspWriter class will throw IOExceptions exceptions, whereas PrintWriter will 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); | Output the value of Type type |
out.println(dataType dt); | Output the value of Type type followed by a newline |
out.flush(); | Refresh output stream |
The session object is an instance of the javax.servlet.http.HttpSession class and has the same behavior as the session object in Java Servlets.
The session object is used to track sessions between different client requests.
The application object directly wraps the object of the servlet's ServletContext class, which is an instance of the javax.servlet.ServletContext class.
This object represents the JSP page throughout the entire lifecycle of the JSP page. It is created when the JSP page is initialized and removed when the jspDestroy() method is called.
By adding properties to the application, all JSP files that make up your web application can access these properties.
The config object is an instance of the javax.servlet.ServletConfig class, which directly wraps the object of the servlet's ServletConfig class.
This object allows developers to access initialization parameters of Servlet or JSP engines, such as file paths, etc.
The following is the usage of the config object, which is not very important and therefore not commonly used:
config.getServletName();
It returns the content included in the <servlet-The servlet name in the name>element, note that <servlet-The name>element in WEB-Defined in the INF\web.xml file.
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 and filter out most of the implementation details.
这个对象存储了request对象和response对象的引用。application对象,config对象,session对象,out对象可以通过访问这个对象的属性来导出。
This object stores references to the request object and response objects. 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 the instruction information passed to the JSP page, including cache information, ErrorPage URL, page scope, etc.4The PageContext class defines some fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE. It also provides
There are 0 remaining 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 related attributes in the four scopes, but the following method only removes the related attributes in a specific scope:
This object is a reference to the page example. It can be regarded as the representative of the entire JSP page.
The 'page' object is synonymous with the 'this' object.
The 'exception' object wraps the exception information thrown from the previous page. It is usually used to generate an appropriate response to error conditions.