In this example we are going to explain what are the implicit objects in a jsp page and what is their functionality, as well as show some examples of their usage.
1. Implicit objects in a jsp page
When we are creating a Java web application, we are using some Java specific technologies, more specifically servlets and jsp pages. Jsp pages could be seen as normal html pages which however, can interact with Java backend code, as well as contain some Java code within them, which are called scriptlets.
Implicit objects are 9 specific objects that exist in every jsp page by default and provide multiple functionality depending on the object. Let’s take a look at what are these objects and what they can be used for:
out: It is the implementation of
JspWriterclass and it is used as a convenient and easy output to the jsp client page. It is more or less standard in its usage, and that’s why it is heavily used in the backend as well (servlet) in order to output information after the execution of some method.
page: It is a simple instance of
Object, and essentially represents the current jsp page that the client is using.
request: It is an instance of
HttpServletRequestand represents a request (HTTP), from the client to the server. Each time that a new request is made, a new instance is created representing that exact request. It is used heavily for important information like headers, cookies, etc.
response: In addition to request, we are also using the response object, an instance of
HttpServletResponse, and represents the response of the server to the client.
exception: it is an instance of
Exception, the Java class used for throwing exceptions when something goes wrong with the code. It is rarely used, although in some cases jsp an use it for error pages.
config: It represents the
ServletConfigclass and it is used to access information about the servlet and the initialization of the jsp page.
pageContext: As an instance of the
PageContextclass, it is mainly used to access information about the page, as well as to work with attributes of different scopes and pages.
session: Implementation of the
HttpSessionclass, and it represents the current session of using the jsp page. It represents the scope of this session, and it is useful in order to keep attributes and values and providing them in different jsp pages of the same application.
application: Instance of
ServletContext, and mainly used to keep objects and values in order to access them in different jsp pages.
2. Implicit Objects example
Let’s take a look at an example of some common implicit objects usage.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="java.io.PrintWriter"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Implicit Objects Example</title> </head> <body> <%-- response object --%> <% PrintWriter pw = response.getWriter(); pw.print("Response writer example."); %><br><br> <%-- request object --%> <strong>Request - locale example: </strong><%=request.getLocale().toString() %><br><br> <%-- out object --%> <strong>Out object prints: </strong><%out.print("This is an example");%><br><br> <%-- config object --%> <strong>Config example - servlet name: </strong><%=config.getServletName()%><br><br> <%-- application object --%> <strong>Application example - server info: </strong><%=application.getServerInfo()%><br><br> <%-- page object --%> <strong>Page example - page name: </strong><%=page.getClass().getName()%><br><br> <%-- session object --%> <strong>Session example - creation time: </strong><%=session.getCreationTime()%><br><br> <%-- pageContext object --%> <% pageContext.setAttribute("Test", "Test Value"); %> <strong>PageContext example - class name: </strong><%=pageContext.getClass().getName() %><br><br> </body> </html>
We also need to configure a web.xml file, in order to be able to deploy this jsp page with a server like Tomcat. The web.xml file for this application is:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ImplicitObjectsExample</servlet-name> <jsp-file>/index.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>ImplicitObjectsExample</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
As you can see in the output here, all the relevant information that was asked from the jsp page is presented and available.
3. Download the example
This was an example of implicit objects in a jsp page.
You can download the full source code of this example here : JSPImplicitObjectsExample