In the earlier tutorial, we have seen JSP Scripting Elements with Example. In this tutorial, we will discuss & understand JSP implicit objects with examples. Primarily, there are 9 implicit objects in JSP. Let’s learn all these implicit objects in detail. But first, learn what is JSP implicit objects?
This Implicit Objects in JSP Tutorial Contains:
- What are JSP Implicit Objects?
- Implicit Objects and their corresponding classes
- How many Implicit Objects are available in JSP?
Implicit objects are the set of predefined objects readily available for use. These objects are created by the JSP container, while a JSP page translates into Servlet. Implicit Objects are being created inside the service() method so we can use implicit objects directly within scriptlets without initializing and declaring them. Total 9 Implicit objects are available in JSP.
- JSP Standard Tag Library Tutorial (JSTL), JSTL Tags
- JSP Action Tags – jsp useBean, jsp include, jsp forward
There are 9 types of implicit objects available in the container:
Let’s check out one by one from the below sections:
It is used for writing content to the client browser. It is an object of the JSP Writer. In servlet you need to write PrintWriter out = response.getWriter() for writing content to the browser, but in JSP you don’t need to write this.
It is an object of HttpServletRequest.This object is created for each JSP request by the web container. The main purpose of this object is to get the data on a JSP page which is entered by the user. This object is used to get request information like a parameter, header information, content type, server name, etc.
It is an object of HttpServletResponse. This object will be created by the container for each request. We can use the response object to set content type, adding cookies, and redirecting the request to another resource.
JSP page implicit object is an object of java.lang.Object class. This object represents the current JSP page. It is rarely used. It provides a reference to the generated servlet class.
JSP pageContext object is an object of javax.servlet.jsp.PageContext class. It is used for accessing page, application, request and session scope.
JSP config implicit object is an instance of java.servlet.ServletConfig class. This object is created by the container for each JSP page. It is used to get the initialization parameter in the deployment descriptor(web.xml) file.
JSP application implicit object is an instance of java.servlet.servletContext class.It is used to obtain the context information and attributes in JSP.The servlet context obtained from the servlet configuration object by using the getServletConfig().getServletContext() method.One application object is created by the container for one JSP application when the application is deployed.
JSP Session implicit object is an instance of javax.servlet.http.HttpSession class.The session object is created for the requesting clients(if any). This variable is only valid for HTTP-based protocol. we can use this object to get, set, and remove an attribute from the session scope.
JSP exception implicit object is an instance of java.lang.Throwable class. It is used for exception handling in JSP. It can be only used for the JSP error page.