Skip to main content

Command Palette

Search for a command to run...

🌐 Introduction to JSP (Java Server Pages)

Published
🌐 Introduction to JSP (Java Server Pages)
S

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:

  1. Translation: JSP is translated into a Servlet.

  2. Compilation: The Servlet is compiled into bytecode.

  3. Loading & Instantiation: The compiled class is loaded into memory and an object is created.

  4. Initialization: jspInit() method is called once.

  5. Request Handling: jspService() handles requests/responses.

  6. Destruction: jspDestroy() cleans up resources before unloading.

πŸ›  Lifecycle Methods:

  • jspInit() β†’ Called once, similar to init() 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:

  1. The web container translates JSP into a Servlet.

  2. The Servlet is compiled into Java bytecode.

  3. The compiled Servlet executes, generating dynamic HTML.

  4. 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; %>

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:

ObjectTypePurpose
outPrintWriterSends output to client
requestHttpServletRequestRequest data
responseHttpServletResponseResponse handling
configServletConfigConfig info
sessionHttpSessionUser session data
applicationServletContextApp-wide data
pageContextPageContextPage-specific data
pageObjectCurrent JSP instance
exceptionThrowableError handling (error pages)

πŸ”„ Difference Between Servlet and JSP

Both JSP and Servlets are server-side technologies, but they serve different purposes.

Servlet

  1. Pure Java code.

  2. Harder to write UI in HTML inside Java.

  3. Runs faster than JSP (no translation overhead).

  4. Works as Controller in MVC.

  5. Accepts all protocol requests (HTTP, HTTPS, etc.).

  6. Lifecycle: init(), service(), destroy().

JSP

  1. Primarily HTML with optional Java.

  2. Easier for presentation logic.

  3. Slower than Servlets (extra translation/compilation).

  4. Works as View in MVC.

  5. Accepts only HTTP requests.

  6. 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

FeatureHTML (Static)JSP (Dynamic)
NatureStatic pagesDynamic pages
Server needed❌ Noβœ… Yes
Java Code❌ Not allowedβœ… Allowed
Runs inBrowserWeb Server
Language typeClient-sideServer-side
SpeedVery fastSlower (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.

More from this blog

T

Tech Chronicles by Suraj

19 posts

Exploring new tech, AI tools, and software development trends. Sharing insights, tutorials, and discoveries for developers and tech enthusiasts.