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).
<%@ 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:
| Feature | JSP | Servlet |
|---|---|---|
| Primary Use | Presentation layer (View) | Business/Controller logic |
| File Extension | .jsp | .java (compiled to .class) |
| Code Style | HTML with embedded Java | Java with embedded HTML |
| Compilation | Auto-compiled by container | Must be compiled manually |
| Implicit Objects | 9 built-in implicit objects | Must be created explicitly |
| Custom Tags | Supported (JSTL, custom) | Not supported |
| Maintenance | Easier (HTML-centric) | Harder (Java-centric) |
| Performance | Slightly slower (first request) | Faster (pre-compiled) |
| MVC Role | View | Controller |
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:
- Translation: The JSP container translates the
.jspfile into a Java Servlet source file (.java). This happens only once (or when the JSP is modified). - Compilation: The generated
.javafile is compiled into a.classbytecode file by the Java compiler. - Loading: The compiled class is loaded into the JVM by the class loader.
- Instantiation: An instance of the Servlet class is created by the container.
- Initialization: The
jspInit()method is called once to initialize the JSP. You can override this to set up resources. - Request Processing: For each client request, the
_jspService()method is called. This is where the actual response is generated. - Destroy: When the JSP is taken out of service,
jspDestroy()is called to release resources.
<%@ 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
.jspresource. - 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.