Nearby lessons
7 of 30JSP - Implicit Objects
- 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
| Object | Type | Purpose |
|---|---|---|
request | HttpServletRequest | Read request parameters and headers |
response | HttpServletResponse | Set response headers and content type |
config | ServletConfig | Read servlet init parameters |
application | ServletContext | Read context parameters and app-level data |
session | HttpSession | Access and manage user sessions |
out | JspWriter | Write output to the response |
page | Object | Reference to the current servlet instance |
pageContext | PageContext | Gateway to all objects and scopes |
exception | Throwable | Available 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.
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:
3) session (HttpSession)
The session object is available by default and is of type HttpSession. All HttpSession methods are available.
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.
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:
- Get all other JSP implicit objects
- Perform request dispatching (forward/include)
- Perform attribute management in any scope
pageContext — Accessing Implicit Objects
pageContext acts as a single point of contact for all other implicit objects:
| Object | pageContext Method |
|---|---|
| request | getRequest() |
| response | getResponse() |
| config | getServletConfig() |
| application | getServletContext() |
| session | getSession() |
| out | getOut() |
| page | getPage() |
| exception | getException() |
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:
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:
| Feature | JspWriter (out) | PrintWriter |
|---|---|---|
| Buffering | Yes (buffered) | No (writes directly to response) |
| Methods | print(), 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):
7) page
The page implicit object always points to the current servlet instance. In the generated servlet, it is declared as:
page Rules
pageis declared with typeObject.- You can only call methods from
Objectclass — no servlet-specific methods. - Use type-casting to call servlet methods.
- This is the most rarely used implicit object.
- 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