Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

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.

ObjectTypeScopeDescription
outJspWriterPageOutput stream to write to the response
requestHttpServletRequestRequestClient's HTTP request data
responseHttpServletResponsePageHTTP response to the client
sessionHttpSessionSessionUser's session data
applicationServletContextApplicationShared application-wide data
configServletConfigPageServlet configuration parameters
pageContextPageContextPageAccess to all other implicit objects
pageObject (this)PageReference to the current Servlet instance
exceptionThrowablePageException (only on error pages)
request and response Objects
<%@ 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>
session and application Objects
<%@ 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

out, config, 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.