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
Loading editor…

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
Loading editor…

3) session (HttpSession)

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

Example04
Loading editor…

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
Loading editor…

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
Loading editor…

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
Loading editor…

7) page

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

Example13
Loading editor…

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
Loading editor…
📝 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.