JSP Implicit Objects
What are Implicit Objects?
JSP provides 9 built-in objects that are automatically available in every JSP page without any declaration. These objects are created by the JSP container and can be used directly in scriptlets and expressions.
| Object | Type | Scope | Description |
|---|---|---|---|
out | JspWriter | Page | Output stream to write to the response |
request | HttpServletRequest | Request | Client's HTTP request data |
response | HttpServletResponse | Page | HTTP response to the client |
session | HttpSession | Session | User's session data |
application | ServletContext | Application | Shared application-wide data |
config | ServletConfig | Page | Servlet configuration parameters |
pageContext | PageContext | Page | Access to all other implicit objects |
page | Object (this) | Page | Reference to the current Servlet instance |
exception | Throwable | Page | Exception (only on error pages) |
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html><body>
<h3>Request Object</h3>
<%
// Get request parameters
String name = request.getParameter("name");
String age = request.getParameter("age");
// Get request headers
String userAgent = request.getHeader("User-Agent");
String method = request.getMethod();
String uri = request.getRequestURI();
String remoteIP = request.getRemoteAddr();
%>
<p>Name: <%= name %></p>
<p>Age: <%= age %></p>
<p>Method: <%= method %></p>
<p>URI: <%= uri %></p>
<p>Client IP: <%= remoteIP %></p>
<p>User-Agent: <%= userAgent %></p>
<h3>Response Object</h3>
<%
// Set response headers
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/html; charset=UTF-8");
// Redirect (uncomment to use):
// response.sendRedirect("other.jsp");
%>
<p>Content type set to: text/html; charset=UTF-8</p>
</body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html><body>
<h3>Session Object</h3>
<%
// Store data in session
session.setAttribute("username", "Alice");
session.setAttribute("role", "admin");
// Retrieve session data
String username = (String) session.getAttribute("username");
String sessionId = session.getId();
long creationTime = session.getCreationTime();
%>
<p>Username: <%= username %></p>
<p>Session ID: <%= sessionId %></p>
<p>Session Created: <%= new java.util.Date(creationTime) %></p>
<h3>Application Object (ServletContext)</h3>
<%
// Store application-wide data
application.setAttribute("appName", "My JSP App");
application.setAttribute("version", "1.0");
// Get server info
String serverInfo = application.getServerInfo();
String appName = (String) application.getAttribute("appName");
%>
<p>App Name: <%= appName %></p>
<p>Server: <%= serverInfo %></p>
<p>Context Path: <%= application.getContextPath() %></p>
</body></html>
out, config, and pageContext Objects
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html><body>
<h3>out Object (JspWriter)</h3>
<%
out.println("<p>Written using out.println()</p>");
out.print("<p>Buffer size: " + out.getBufferSize() + " bytes</p>");
out.flush(); // Flush the buffer
%>
<h3>config Object (ServletConfig)</h3>
<%
String servletName = config.getServletName();
// Get init parameters defined in web.xml
String dbUrl = config.getInitParameter("dbUrl");
%>
<p>Servlet Name: <%= servletName %></p>
<h3>pageContext Object</h3>
<%
// pageContext provides access to all scopes
pageContext.setAttribute("localVar", "page scope value");
pageContext.setAttribute("sessionVar", "session value", PageContext.SESSION_SCOPE);
pageContext.setAttribute("appVar", "app value", PageContext.APPLICATION_SCOPE);
// Retrieve from specific scope
String local = (String) pageContext.getAttribute("localVar");
String sess = (String) pageContext.getAttribute("sessionVar", PageContext.SESSION_SCOPE);
%>
<p>Local: <%= local %></p>
<p>Session: <%= sess %></p>
</body></html>
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.