Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP implicit object


May 12, 2021 JSP


Table of contents


JSP implicit object

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 implicit objects supported by JSP:

Object Describe
request An instance of the HttpServletRequest class
response An instance of the HttpServletResponse class
out An instance of the PrintWriter class that outputs the results to a Web page
session An instance of the HttpSession class
application An instance of the ServletContext class, related to the application context
config An instance of the ServletConfig class
pageContext An instance of the PageContext class that provides access to all objects and namespaces of JSP pages
page Similar to the this keyword in a Java class
Exception The object of the Exception class, which represents the corresponding exception object on the JSP page where the error occurred

The request object

The request object is an instance of the javax.servlet.http://httpservletRequest class. Every time a client requests a JSP page, the JSP engine creates a new request object to represent the request.

The request object provides a range of methods for obtaining HTTP header information, cookies, HTTP methods, and more.


The response object

The response object is an example of the javax.servlet.http://httpsponse class. When the server creates a request object, it also creates a response object that responds to the 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.


Out object

The out object is an instance of the javax.servlet .jsp.JspWriter class used to write in the response object.

The original JspWriter class object performs different instantiation operations depending on whether the page has a cache. You can easily close the cache by using the buffered'false' property in the page directive.

The JspWriter class contains most of the methods in the java.io.PrintWriter class. H owever, JspWriter has added new methods designed to handle caching. Also, the JspWriter class throws the IOExs exception, whereaprintWriter 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:

Method Describe
out.print(dataType dt) Outputs the value of the Type Type
out.println(dataType dt) Output the value of the Type Type and line up
out.flush() Refresh the output stream

The session object

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

The session object is used to track sessions between client requests.


The application object

The application object wraps the object of the servletContext class directly and is an example of the javax.servlet.servletContext class.

This object represents the JSP page throughout its lifecycle. This object was created when the JSP page was initialized and removed with the call of the jspDestroy() method.

By adding properties to the application, they are accessible to all the JSP files that make up your web app.


Config object

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

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

Here's how the confeg object is used, not very important, so it's not commonly used:

config.getServletName();

It returns the name of the servlet contained in the element, note that the element is defined in the WEB-INF.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 primarily used to access page information while filtering out most of the implementation details.

This object stores references to the request object and the response object. Application objects, config objects, session objects, out objects can be exported by accessing the properties of this object.

The pageContext object also contains instruction information passed to the JSP page, including cached information, ErrorPage URL, page scope, etc.

The PageContext class defines a number of fields, including PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, APPLICATION_SCOPE. It also provides more than 40 methods, half of which are inherited from the javax.servlet .jsp.JspContext class.

One important method is removeArribute(), which accepts one or two parameters. F or example, pageContext.removeArribute ("attrName") removes related properties from four scopes, but the following approach removes only related properties in a particular scope:

pageContext.removeAttribute("attrName", PAGE_SCOPE);

Page object

This object is a reference to the page instance. It can be seen as representing the entire JSP page.

The page object is synonym for the this object.


The exception object

The exception object wraps up the exception information thrown from the previous page. It is often used to produce an appropriate response to an error condition.