JSP implicit object


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.

Nine implicit objects supported by JSP:

ObjectDescription
requestHttpServletRequestInstance of class
responseHttpServletResponseInstance of class
outInstance of PrintWriter class, used to output the results to the web page
sessionHttpSessionInstance of class
applicationInstance of the ServletContext class, related to the application context
configServletConfigInstance of class
pageContextInstance of the PageContext class, providing access to all objects and namespaces of the JSP page
page Similar to the this keyword in Java classes
An object of the ExceptionException 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 the 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 the client.

The response object also defines the interface for processing 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 and is used to write content in the response object.

The initial JspWriter class object performs different instantiation operations depending on whether the page is cached. Caching can be easily turned off 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 has added some new methods specifically designed to handle caching. Also, the JspWriter class will throw IOExceptions, but PrintWriter will not.

The following table lists the important methods we will use to output boolean, char, int, double, String, object and other types of data:

Output the value of Type type Output the Type type value and then wrap the line##out.flush()

session object

The session object is an instance of the javax.servlet.http.HttpSession class. Has the same behavior as the session object in Java Servlets.

The session object is used to track the session between each client request.


application object

The application object directly wraps the object of the ServletContext class of the servlet and is an instance of the javax.servlet.ServletContext class.

This object represents this JSP page throughout the entire life cycle of the JSP page. This object is created when the JSP page is initialized and is 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.


config object

The config object is an instance of the javax.servlet.ServletConfig class, which directly wraps the object of the ServletConfig class of the servlet.

This object allows developers to access the initialization parameters of the Servlet or JSP engine, such as file paths, etc.

The following 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 <servlet The -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 and is 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 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 the instruction information passed to the JSP page, including cache 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 inherit from the javax.servlet.jsp.JspContext class.

One of the important methods is removeArribute(), which accepts one or two parameters. For example, pageContext.removeArribute("attrName") removes related attributes in four scopes, but the following method only removes related attributes in 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 representative of the entire JSP page.

The page object is a synonym for this object.


exception object

The exception object wraps exception information thrown from the previous page. It is often used to generate appropriate responses to error conditions.

MethodDescription
##out.print(dataType dt)
out.println(dataType dt)
Refresh the output stream