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:
object | describe |
---|---|
request | Instance of HttpServletRequest class |
response | Instance of HttpServletResponse class |
out | An instance of the PrintWriter class, used to output results to a web page |
session | Instance of 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, providing access to all objects and namespaces of the JSP page |
page | Similar to this keyword in Java classes |
Exception | An object of the Exception class represents 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 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 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.
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:
method | describe |
---|---|
out.print(dataType dt) | Output type value |
out.println(dataType dt) | Output the value of Type type and then wrap the line |
out.flush() | Flush the output stream |
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 sessions between client requests.
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 the JSP page throughout its entire life cycle. This object is created when the JSP page is initialized and is removed when the jspDestroy() method is called.
By adding properties to your application, these properties are accessible to all JSP files that make up your web application.
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 the <servlet-name> element is defined in the WEB-INFweb.xml file.
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);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.
The exception object wraps exception information thrown from the previous page. It is often used to generate appropriate responses to error conditions.