Ilias Koutsakis

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

JSP Implicit Objects Example

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.

2. Implicit Objects example

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

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

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.

Download
You can download the full source code of this example here : JSPImplicitObjectsExample

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close