Introduction to JSP

Module: B3.3-R5: Web Technologies

Chapter: JSP And Database Connectivity

Introduction to JavaServer Pages (JSP)

JavaServer Pages (JSP) is a server-side technology used to create dynamic, platform-independent web applications. It allows developers to embed Java code directly inside HTML pages using JSP tags and scripting elements.

JSP is built on top of Java Servlets and offers a simpler, template-based approach to generate dynamic content.

Why JSP?

  • Easy to embed Java code into HTML
  • Supports MVC architecture
  • Simplifies presentation logic
  • Reusable components using tags and JSTL
  • Powerful backend integration using JDBC and JavaBeans

1. What is JSP?

JSP is a technology that extends Servlets and enables dynamic web page creation using Java. During execution:

  1. A JSP page is converted into a Servlet by the JSP engine
  2. The Servlet is compiled into bytecode
  3. The compiled Servlet generates dynamic output
JSP File Extension

.jsp

2. Basic Syntax of a JSP Page

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<body>
    <h2>Welcome to JSP</h2>
    Current Time: <%= new java.util.Date() %>
</body>
</html>

3. JSP vs Servlet

Feature Servlet JSP
Programming Style Pure Java Java + HTML mixed
Best For Business logic Presentation layer
Compilation Directly compiled Converted to Servlet then compiled
Ease of Use Harder Simple template system

4. Features of JSP

  • Implicit Objects – Predefined objects like request, response, session, application
  • Automatic Compilation – JSP is converted into Servlets automatically
  • Tag-based development – using JSTL and custom tags
  • Reusability – JavaBeans integration
  • Scalable – integrates with JDBC, Hibernate, Spring
  • Supports MVC – separates logic (Servlet) and view (JSP)

5. Life Cycle Overview

JSP lifecycle includes:

  1. Translation → JSP → Servlet
  2. Compilation → Class file
  3. Loading the Servlet
  4. Instantiation
  5. Initialization (jspInit())
  6. Request Handling (_jspService())
  7. Destroy (jspDestroy())
Lifecycle Diagram
JSP Page
   ↓ (Translation)
Servlet Source
   ↓ (Compilation)
Servlet Class
   ↓
Loaded by Container
   ↓
jspInit() → _jspService() → jspDestroy()

6. Advantages of JSP

  • Easy to develop dynamic web pages
  • Reduces code complexity compared to Servlets
  • Automatic JavaBean and JDBC integration
  • Extensive tag library support
  • Faster development cycle

7. Practical Example – Dynamic Greeting Page

<%@ page import="java.util.*" %>
<html>
<body>
<h2>Dynamic Greeting</h2>

<%
   Date d = new Date();
   out.println("Current Date & Time: " + d);
%>

</body>
</html>

Conclusion

JSP is a powerful, flexible, and easy-to-use server-side technology designed to simplify the creation of dynamic web pages. By combining HTML with Java, JSP provides a fast and efficient way to build scalable and interactive web applications.