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:
Object | Description |
---|---|
request | HttpServletRequestInstance of class |
response | HttpServletResponseInstance of class |
out | Instance of PrintWriter class, used to output the results to the web page |
session | HttpSessionInstance of class |
application | Instance of the ServletContext class, related to the application context |
config | ServletConfigInstance of class |
pageContext | Instance 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 Exception | Exception 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:
Method | Description |
---|---|
##out.print(dataType dt) | Output the value of Type type|
out.println(dataType dt) | Output the Type type value and then wrap the line|
Refresh the output stream |