JSP Implicit Objects Example (original) (raw)

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:

  1. out: It is the implementation of JspWriter class 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.
  2. page: It is a simple instance of Object, and essentially represents the current jsp page that the client is using.
  3. request: It is an instance of HttpServletRequest and 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.
  4. 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.
  5. 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.
  6. config: It represents the ServletConfig class and it is used to access information about the servlet and the initialization of the jsp page.
  7. pageContext: As an instance of the PageContext class, it is mainly used to access information about the page, as well as to work with attributes of different scopes and pages.
  8. session: Implementation of the HttpSession class, 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.
  9. application: Instance of ServletContext, and mainly used to keep objects and values in order to access them in different jsp pages.

Let’s take a look at an example of some common implicit objects usage.
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.io.PrintWriter"%>

<title>Implicit Objects Example</title>
<%-- response object --%> <% PrintWriter pw = response.getWriter(); pw.print("Response writer example."); %>

<%-- request object --%> Request - locale example: <%=request.getLocale().toString() %>

<%-- out object --%> Out object prints: <%out.print("This is an example");%>

<%-- config object --%> Config example - servlet name: <%=config.getServletName()%>

<%-- application object --%> Application example - server info: <%=application.getServerInfo()%>

<%-- page object --%> Page example - page name: <%=page.getClass().getName()%>

<%-- session object --%> Session example - creation time: <%=session.getCreationTime()%>

<%-- pageContext object --%> <% pageContext.setAttribute("Test", "Test Value"); %> PageContext example - class name: <%=pageContext.getClass().getName() %>

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:
web.xml

<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>

Output

As you can see in the output here, all the relevant information that was asked from the jsp page is presented and available.

The result of using implicit objects directly in a jsp page.

The result of using implicit objects directly in a jsp page.

3. Download the example

This was an example of implicit objects in a jsp page.

Photo of Ilias Koutsakis

Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.

Back to top button