🧠 What is JSP in Java?
JSP (JavaServer Pages) is a server-side technology that allows embedding Java code inside HTML
pages to build dynamic web content.
JSP is a part of Java EE (Jakarta EE) and works as an abstraction over Servlets — making it easier to
develop the view layer (UI) in Java web applications.
✅ Why Use JSP?
Feature Explanation
Easy to maintain Separates UI (HTML) from logic (Java)
HTML-friendly Designers can work on UI with little Java
Dynamic content Output changes based on user input, DB, etc.
Auto-compiles Converted to Servlet automatically
Java power Can use full Java logic if needed
🔁 How JSP Works Internally
1. User sends a request to a .jsp file
2. JSP Engine converts it to a Servlet (Java class)
3. Servlet is compiled into bytecode
4. Servlet runs and generates HTML
5. Response is sent back to the browser
✅ So every JSP page is just a shortcut to writing a servlet.
🧠 JSP Lifecycle
Phase Method Called
1⃣ Translation JSP → Servlet (.java)
2⃣ Compilation Servlet .java → .class
3⃣ Initialization jspInit()
4⃣ Request Handling _jspService()
5⃣ Destruction jspDestroy()
🧠 JSP Tags and Elements
1. Scriptlet Tag – <% ... %>
Used to write Java code (if, loops, etc.)
<%
int a = 5;
[Link](a * a);
%>
2. Expression Tag – <%= ... %>
Used to print a value
<%= "Hello " + [Link]("name") %>
3. Declaration Tag – <%! ... %>
Used to declare methods or variables at class level
<%! int counter = 0; %>
<%! public int square(int x) { return x * x; } %>
4. Directive Tag – <%@ ... %>
Used to give instructions to JSP engine
<%@ page import="[Link]" %>
<%@ include file="[Link]" %>
5. Action Tag – <jsp:... />
Used to perform specific actions like include, forward, useBean
<jsp:useBean id="user" class="[Link]" />
<jsp:setProperty name="user" property="*" />
<jsp:getProperty name="user" property="name" />
6. JSTL Tags (Advanced)
Use taglib directive to include JSTL libraries and avoid Java code in JSP.
<%@ taglib uri="[Link] prefix="c" %>
<c:if test="${marks > 40}">Pass</c:if>
🗂️ Implicit Objects in JSP
These are predefined objects available in every JSP page.
Object Description
request HTTP request object
response HTTP response object
session User session
application ServletContext
out Writes to response
config ServletConfig
pageContext Context for current page
exception Used in error pages
page Reference to current JSP (this)
💼 Use Case: JSP vs Servlet
Feature JSP Servlet
Best for UI (View Layer) Logic/Controller Layer
Code Mix of HTML + Java Pure Java
Readability Easier for designers Hard for designers
Compilation Auto-translated to Servlet Hand-coded
📦 Example: Simple Login with JSP
📁 [Link]
<form action="[Link]" method="post">
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" value="Login">
</form>
📁 [Link]
<%
String u = [Link]("uname");
String p = [Link]("pass");
if([Link]("admin") && [Link]("1234")) {
%>
<h2>Welcome, <%= u %>!</h2>
<%
} else {
%>
<h2 style="color:red;">Login Failed!</h2>
<%
%>
📦 Tools for JSP Development
Tool Purpose
Eclipse IDE for Java EE development
Tomcat Web server to run JSP/Servlet
MySQL Database for web apps
JDBC Java API to connect to DB
Apache POI Export JSP data to Excel
🚀 Want to Practice?
I can give you:
JSP mini projects (e.g., Login + Register + Dashboard)
CRUD with JSP + JDBC
JSP + Servlet + Bean full example
Let me know what area you want to focus on next:
JSP + Database
JSP + Servlet
JSP + Bean
Full mini project with folder structure
I'll prepare it with step-by-step guidance.