π Introduction to JSP (Java Server Pages)

I'm Suraj Parmeshwar Shinde, a passionate software developer from Tadshivani, Maharashtra, currently based in Pune. Iβve recently completed my Bachelor of Computer Applications (BCA) from Badrinarayan Barwale College, Jalna. During my graduation, I worked as a Software Intern at PRYM Aerospace Pvt. Ltd., where I contributed to the development of an AI-based crop advisory platform using technologies like Node.js, Flask, and React.js. This experience helped me gain hands-on knowledge of real-world software development and agile practices. For my final year project, I built Medicheck, a full-stack doctor appointment booking system using the MERN stack and Tailwind CSS. It features patient and admin panels, doctor profiles, secure Razorpay payments, and a mobile-responsive interface. My core technical skills include React.js, Node.js, Express.js, JavaScript, Java, MongoDB, SQL, and tools like Git, Postman, Docker, and Netlify. Iβm a quick learner who enjoys building real-world applications, solving logical problems, and writing clean, maintainable code. Outside of tech, I enjoy driving and reading books. Iβm always eager to grow, collaborate, and contribute to impactful technology solutions.
Java Server Pages
Web development has evolved massively over the last few decades. While modern frameworks like Spring Boot, Angular, and React dominate the market today, Java Server Pages (JSP) played a crucial role in building dynamic web applications in the early 2000s.
JSP is a server-side technology that allows developers to combine HTML with Java code (Servlets) to generate dynamic content for websites. Even though JSP is considered outdated in modern architectures, it is still widely used in many legacy applications and is important to understand for Java developers.
In this article, weβll dive deep into JSPβits purpose, lifecycle, tags, implicit objects, comparisons with Servlets and HTML, and its role in the MVC architecture.
β‘ What is JSP?
JSP (Java Server Pages) is a technology that enables the creation of platform-independent dynamic web pages using HTML, JSP tags, and embedded Java code.
π Key Points:
JSP is an advanced version of Servlet technology.
Internally, JSP pages are converted into Servlets by the web container (like Tomcat).
Primarily used for presentation logic (displaying UI), though it can include business logic (not recommended).
Best practice: Keep business logic inside Java Beans or the backend layer.
Supports only HTTP methods (GET, POST, HEAD).
File extension:
.jsp.Can contain HTML, JSP tags, custom tags, and Java code (though embedding Java directly in JSP is discouraged).
βοΈ JSP Lifecycle
Like Servlets, JSP also goes through a defined lifecycle managed by the web container.
β Phases of JSP Lifecycle:
Translation: JSP is translated into a Servlet.
Compilation: The Servlet is compiled into bytecode.
Loading & Instantiation: The compiled class is loaded into memory and an object is created.
Initialization:
jspInit()method is called once.Request Handling:
jspService()handles requests/responses.Destruction:
jspDestroy()cleans up resources before unloading.
π Lifecycle Methods:
jspInit()β Called once, similar toinit()in Servlets.jspService()β Handles each client request.jspDestroy()β Called once when the JSP is removed.
π How JSP Works
When a client requests a .jsp page:
The web container translates JSP into a Servlet.
The Servlet is compiled into Java bytecode.
The compiled Servlet executes, generating dynamic HTML.
The HTML response is sent back to the client.
π In short: JSP β Servlet β Bytecode β HTML Response
π· JSP Tags
JSP provides different tags for writing code:
1. Directive Tag
Provides page-level instructions.
<%@ page import="java.util.*" %>
2. Declarative Tag
Declares variables or methods.
<%! int counter = 0; %>
3. Scriptlet Tag (β Not recommended)
Embeds Java code.
<% out.println("Hello, JSP!"); %>
4. Expression Tag
Evaluates and outputs a value.
<%= new java.util.Date() %>
5. Action Tag
Used for including files or forwarding requests.
<jsp:include page="header.jsp" />
6. Custom Tags
Extend JSP functionality via tag libraries.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="Hello World!" />
π JSP Implicit Objects
JSP provides 9 built-in implicit objects, making coding easier:
| Object | Type | Purpose |
out | PrintWriter | Sends output to client |
request | HttpServletRequest | Request data |
response | HttpServletResponse | Response handling |
config | ServletConfig | Config info |
session | HttpSession | User session data |
application | ServletContext | App-wide data |
pageContext | PageContext | Page-specific data |
page | Object | Current JSP instance |
exception | Throwable | Error handling (error pages) |
π Difference Between Servlet and JSP
Both JSP and Servlets are server-side technologies, but they serve different purposes.
Servlet
Pure Java code.
Harder to write UI in HTML inside Java.
Runs faster than JSP (no translation overhead).
Works as Controller in MVC.
Accepts all protocol requests (HTTP, HTTPS, etc.).
Lifecycle:
init(),service(),destroy().
JSP
Primarily HTML with optional Java.
Easier for presentation logic.
Slower than Servlets (extra translation/compilation).
Works as View in MVC.
Accepts only HTTP requests.
Lifecycle:
jspInit(),jspService(),jspDestroy().
π JSP in MVC Architecture
JSP fits into the MVC (Model-View-Controller) pattern as the View layer.
Model β Handles business logic & database (JDBC, Hibernate).
View β JSP (for UI rendering).
Controller β Servlets, Spring, or Spring Boot (request handling).
π Example Setup:
Frontend: JSP / HTML / Angular / React
Backend: Servlet / Spring / Spring Boot
Database: JDBC, ORM (Hibernate)
π JSP vs HTML
| Feature | HTML (Static) | JSP (Dynamic) |
| Nature | Static pages | Dynamic pages |
| Server needed | β No | β Yes |
| Java Code | β Not allowed | β Allowed |
| Runs in | Browser | Web Server |
| Language type | Client-side | Server-side |
| Speed | Very fast | Slower (server processing) |
π Conclusion
JSP was a game-changer in early Java web development, making it easier to mix Java with HTML to create dynamic applications. However, modern practices recommend separating business logic from presentation (using frameworks like Spring MVC, Thymeleaf, or frontend frameworks).
While JSP is considered legacy technology, itβs still valuable to learnβespecially for maintaining older enterprise applications.
π If youβre starting fresh, focus on modern Java frameworks (Spring Boot, Thymeleaf) but keep JSP knowledge handy for interviews and legacy systems.



