Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Java Server Pages (JSP): Introduction and Implicit Objects

Lesson 45 of 46 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

Java Server Pages (JSP): Introduction and Implicit Objects

What is JSP?

JSP (Java Server Pages) lets you embed Java code directly inside HTML to generate dynamic web content, instead of writing HTML inside Java println statements as in Servlets. Every JSP page is internally translated into a Servlet and compiled by the container the first time it's requested.

JSP vs Servlet

FeatureServletJSP
ContentJava code with embedded HTML (out.println)HTML with embedded Java
Best forBusiness logic, control flowPresentation / view layer
CompilationCompiled by developerAuto-compiled to a servlet by the container
EditingRequires recompilation on changeContainer auto-recompiles on change

A First JSP Example

<%-- hello.jsp --%>
<html>
<body>
    <h1>Hello, JSP!</h1>
    <p>Today's date is: <%= new java.util.Date() %></p>
    <%
        int x = 5, y = 10;
        int sum = x + y;
    %>
    <p>Sum of <%= x %> and <%= y %> is <%= sum %></p>
</body>
</html>

Saved as hello.jsp and dropped into a web application's directory — no manual compilation needed; the container handles translation and compilation automatically.

JSP Life Cycle (Translation and Compilation)

  1. Translation – The JSP file is translated into a Java servlet source file.
  2. Compilation – That servlet source is compiled into a .class file.
  3. Loading & Instantiation – The container loads and instantiates the generated servlet.
  4. InitializationjspInit() runs once.
  5. Request Processing_jspService() runs for every request.
  6. DestructionjspDestroy() runs once on shutdown.

JSP Implicit Objects

The container automatically makes these 9 implicit objects available in every JSP page — no need to declare or instantiate them:

Implicit ObjectTypePurpose
requestHttpServletRequestAccess request parameters, headers
responseHttpServletResponseControl the HTTP response
outJspWriterWrite output to the response body
sessionHttpSessionTrack user session data
applicationServletContextApplication-wide (shared) data
configServletConfigJSP's configuration data
pageContextPageContextAccess to all other implicit objects and page-scoped attributes
pageObject (this)Reference to the current JSP instance
exceptionThrowableAvailable only on designated error pages

Using Implicit Objects

<%
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");

    session.setAttribute("lastVisit", new java.util.Date());

    application.setAttribute("visitorCount",
        ((Integer) application.getAttribute("visitorCount")) + 1);
%>

<p>Hello, <%= name %>!</p>
<p>Session ID: <%= session.getId() %></p>
<% out.println("Written via the out object."); %>
Key Takeaway: JSP flips the Servlet model — HTML-first with embedded Java — and is auto-translated/compiled into a servlet behind the scenes. Nine implicit objects (request, response, out, session, application, config, pageContext, page, exception) are always available without any setup.