Nearby lessons

7 of 30

JSP - Implicit Objects

📌 What You Will Learn
  • Know all 9 JSP implicit objects and their types
  • Use request, response, session, and application objects
  • Master the pageContext object and its 3 purposes

JSP Implicit Objects are pre-defined objects available in every JSP without explicit declaration. There are 9 implicit objects, each with a specific role in handling requests, responses, sessions, and more.

All 9 Implicit Objects

ObjectTypePurpose
requestHttpServletRequestRead request parameters and headers
responseHttpServletResponseSet response headers and content type
configServletConfigRead servlet init parameters
applicationServletContextRead context parameters and app-level data
sessionHttpSessionAccess and manage user sessions
outJspWriterWrite output to the response
pageObjectReference to the current servlet instance
pageContextPageContextGateway to all objects and scopes
exceptionThrowableAvailable only on error pages

1) request and response

These are the same objects passed to the service() method. All methods of HttpServletRequest and HttpServletResponse are available.

Example02
JCode Cell
1 
2<h1>
3 The Request Method: <%= request.getMethod() %><br>
4 User Name: <%= request.getParameter("user") %><br>
5 Client IP Address: <%= request.getRemoteAddr() %><br>
6 Content Type: <%= response.getContentType() %>
7</h1>
8

2) application (ServletContext)

The application object is of type ServletContext. It represents the environment of the web application. All methods of ServletContext are available.

Context parameters are defined in web.xml:

Example03
JCode Cell
1 
2web.xml:
3<web-app>
4 <display-name>JSP Implicit Objects Application</display-name>
5 <context-param>
6 <param-name>uname</param-name>
7 <param-value>scott</param-value>
8 </context-param>
9</web-app>
10 
11application.jsp:
12<h1>
13 The context parameter User Name: <%= application.getInitParameter("uname") %><br>
14 The Application Name: <%= application.getServletContextName() %>
15</h1>
16

3) session (HttpSession)

The session object is available by default and is of type HttpSession. All HttpSession methods are available.

Example04
JCode Cell
1 
2session.jsp:
3<h1>
4 The Session ID is: <%= session.getId() %><br>
5 The Session Time out is: <%= session.getMaxInactiveInterval() %> Seconds<br>
6 Is the session newly created: <%= session.isNew() %><br>
7</h1>
8

session Rules

  • Session is available by default in every JSP.
  • To disable it: <%@ page session="false" %>
  • If disabled and you try to use session, you get: CE: session cannot be resolved.

4) config (ServletConfig)

The config object is of type ServletConfig. All ServletConfig methods are available:

  • getServletName()
  • getInitParameter(String pname)
  • getInitParameterNames()
  • getServletContext()

Important: To reflect servlet-level web.xml configurations in a JSP, you must access them using the url-pattern, not the JSP filename.

Example06
JCode Cell
1 
2web.xml:
3<servlet>
4 <servlet-name>DemoJSP</servlet-name>
5 <jsp-file>/config.jsp</jsp-file>
6 <init-param>
7 <param-name>hotTopic</param-name>
8 <param-value>UttarPradesh</param-value>
9 </init-param>
10</servlet>
11<servlet-mapping>
12 <servlet-name>DemoJSP</servlet-name>
13 <url-pattern>/test</url-pattern>
14</servlet-mapping>
15 
16config.jsp:
17The Logical Name: <%= config.getServletName() %><br>
18The Init Param value is: <%= config.getInitParameter("hotTopic") %>
19

5) pageContext — The Gateway

The pageContext object is of type javax.servlet.jsp.PageContext. It is an abstract class — the web server vendor provides the implementation.

It serves 3 purposes:

  1. Get all other JSP implicit objects
  2. Perform request dispatching (forward/include)
  3. Perform attribute management in any scope

pageContext — Accessing Implicit Objects

pageContext acts as a single point of contact for all other implicit objects:

ObjectpageContext Method
requestgetRequest()
responsegetResponse()
configgetServletConfig()
applicationgetServletContext()
sessiongetSession()
outgetOut()
pagegetPage()
exceptiongetException()

These methods are mostly useful in Custom Tag Handlers, not directly in JSPs.

pageContext — Request Dispatching

Use pageContext.forward() or pageContext.include() to dispatch requests:

Example09
JCode Cell
1 
2first.jsp:
3<h1>Hello This is First JSP</h1>
4<% pageContext.forward("second.jsp"); %>
5 
6second.jsp:
7<h1>Hello This is Second JSP</h1>
8 
9Output (forward): Hello This is Second JSP
10Output (include): Hello This is First JSP
11 Hello This is Second JSP
12

pageContext — Attribute Management

pageContext provides methods to manage attributes across all 4 scopes (page, request, session, application). This is covered in detail in the JSP Scopes topic.

6) out (JspWriter)

The out object is of type JspWriter — specially designed for JSPs to write character data to the response.

The main difference between JspWriter and PrintWriter:

FeatureJspWriter (out)PrintWriter
BufferingYes (buffered)No (writes directly to response)
Methodsprint(), println(), write(), close(), flush()print(), println(), write(), close(), flush()

Formula: JspWriter = PrintWriter + Buffer

Do not use both simultaneously in the same JSP — it can cause output ordering issues.

out Example

Example showing buffered output (Mallika appears 3 times because of buffering):

Example12
JCode Cell
1 
2<%@ page import="java.io.*" %>
3<%
4 PrintWriter pw = response.getWriter();
5 out.print("<h1>Mallika</h1>");
6 pw.print("<h1>Sunny</h1>");
7 out.print("<h1>Mallika</h1>");
8 pw.print("<h1>Sunny</h1>");
9 out.print("<h1>Mallika</h1>");
10 pw.print("<h1>Sunny</h1>");
11%>
12 
13Output:
14<h1>Mallika</h1> (from out, buffered)
15<h1>Mallika</h1>
16<h1>Mallika</h1>
17<h1>Sunny</h1> (from pw, direct)
18<h1>Sunny</h1>
19<h1>Sunny</h1>
20
Output

<h1>Mallika</h1>
<h1>Mallika</h1>
<h1>Mallika</h1>
      

7) page

The page implicit object always points to the current servlet instance. In the generated servlet, it is declared as:

Example13
JCode Cell
1 
2Object page = this;
3

page Rules

  • page is declared with type Object.
  • You can only call methods from Object class — no servlet-specific methods.
  • Use type-casting to call servlet methods.
  • This is the most rarely used implicit object.
Example14
JCode Cell
1 
2<%= page.getServletInfo() %> ← CE
3<%= ((HttpServlet)page).getServletInfo() %> ← valid
4
📝 Key Takeaways
  • 9 implicit objects are automatically available in every JSP
  • pageContext is the gateway — it can access all other objects and perform scope management
  • out (JspWriter) has a buffer; PrintWriter does not

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4