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

JSP Introduction

What is JSP?

JSP (Java Server Pages) is a server-side technology that enables developers to create dynamic, platform-independent web content. JSP pages are essentially HTML files with embedded Java code and special JSP tags. When a client requests a JSP page, the server translates it into a Servlet, compiles it, and executes it to produce an HTML response.

JSP was developed by Sun Microsystems (now Oracle) as part of the Java EE (Enterprise Edition) platform. It runs on any JSP-enabled web server such as Apache Tomcat, JBoss, or GlassFish. JSP simplifies web development by separating presentation logic (HTML/CSS) from business logic (Java code).

Basic JSP Page
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>My First JSP Page</title>
</head>
<body>
    <h1>Hello, JSP!</h1>
    <p>Current Date and Time: <%= new java.util.Date() %></p>
    <%
        String name = "World";
        out.println("<p>Hello, " + name + "!</p>");
    %>
</body>
</html>

JSP vs Servlet

JSP and Servlets are both Java-based server-side technologies, but they serve different purposes. Here's a comparison:

FeatureJSPServlet
Primary UsePresentation layer (View)Business/Controller logic
File Extension.jsp.java (compiled to .class)
Code StyleHTML with embedded JavaJava with embedded HTML
CompilationAuto-compiled by containerMust be compiled manually
Implicit Objects9 built-in implicit objectsMust be created explicitly
Custom TagsSupported (JSTL, custom)Not supported
MaintenanceEasier (HTML-centric)Harder (Java-centric)
PerformanceSlightly slower (first request)Faster (pre-compiled)
MVC RoleViewController

JSP Lifecycle

Every JSP page goes through a well-defined lifecycle managed by the JSP container (e.g., Tomcat). Understanding this lifecycle is key to writing efficient JSP applications:

  1. Translation: The JSP container translates the .jsp file into a Java Servlet source file (.java). This happens only once (or when the JSP is modified).
  2. Compilation: The generated .java file is compiled into a .class bytecode file by the Java compiler.
  3. Loading: The compiled class is loaded into the JVM by the class loader.
  4. Instantiation: An instance of the Servlet class is created by the container.
  5. Initialization: The jspInit() method is called once to initialize the JSP. You can override this to set up resources.
  6. Request Processing: For each client request, the _jspService() method is called. This is where the actual response is generated.
  7. Destroy: When the JSP is taken out of service, jspDestroy() is called to release resources.
JSP Lifecycle Methods
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%!
    // jspInit() - called once when JSP is first loaded
    public void jspInit() {
        System.out.println("JSP Initialized");
    }

    // jspDestroy() - called when JSP is removed from service
    public void jspDestroy() {
        System.out.println("JSP Destroyed");
    }
%>
<html>
<body>
    <%
        // _jspService() is called for every request
        out.println("<h2>Processing request...</h2>");
        out.println("<p>Request method: " + request.getMethod() + "</p>");
    %>
</body>
</html>

Advantages of JSP

  • Easy to Learn: Web designers familiar with HTML can write JSP pages without deep Java knowledge.
  • Separation of Concerns: JSP separates presentation (HTML) from business logic (Java), making code more maintainable.
  • Auto-Compilation: The JSP container automatically compiles JSP pages - no manual compilation needed.
  • Implicit Objects: JSP provides 9 built-in objects (request, response, session, etc.) without any setup code.
  • Custom Tags: JSTL and custom tag libraries reduce Java code in JSP pages.
  • Platform Independent: Runs on any JSP-enabled server on any OS.
  • Integration: Seamlessly integrates with JavaBeans, Servlets, JDBC, and other Java EE technologies.

JSP Architecture

JSP follows a request-response architecture within the Java EE web tier:

  • Client (Browser): Sends an HTTP request for a .jsp resource.
  • Web Server / JSP Container: Receives the request. If the JSP hasn't been compiled yet (or has changed), it translates and compiles it into a Servlet.
  • Servlet Engine: Executes the compiled Servlet, which generates the dynamic HTML response.
  • Response: The generated HTML is sent back to the client's browser.

On subsequent requests for the same JSP (unchanged), the container skips translation/compilation and directly executes the cached Servlet class - making JSP very efficient after the first request.


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.