What is JSP?
JSP (JavaServer Pages) is a server-side technology used to create
dynamic web pages in Java.
HTML + Java code = JSP page.
When a JSP page runs, the server converts it into a Servlet.
JSP Tags Overview
JSP uses 3 main types of tags:
1. Directives → Affect the overall structure of servlet (no output).
2. Scripting Elements → Insert Java code inside JSP.
3. Action Tags → Control runtime behavior (forward, include, use
beans, etc).
JSP Directives
Directives affect the entire JSP page and control how the servlet is created.
Types:
page
include
taglib
1. page Directive
Used to provide instructions about the page.
Syntax
<%@ page attribute="value" %>
Common Attributes
language
import
contentType
errorPage
isErrorPage
session
Example
<%@ page import="[Link]" %>
<html>
<body>
Current Date: <%= new Date() %>
</body>
</html>
2. include Directive
Includes a static file at translation time (before servlet creation).
Syntax
<%@ include file="[Link]" %>
Example
<%@ include file="[Link]" %>
<p>This is the home page</p>
3. taglib Directive
Used to include custom tags or JSTL libraries.
Syntax
<%@ taglib uri="[Link] prefix="c" %>
Example
<c:out value="Hello JSTL!" />
JSP Scripting Elements
These are used to write Java code inside JSP.
Types:
1. Declaration <%! ... %>
2. Scriptlet <% ... %>
3. Expression <%= ... %>
1. Declaration Tag
Used to declare variables or methods.
Syntax
<%! int count = 0; %>
<%! public int square(int x){ return x*x; } %>
Example
<%! String college = "MVSR"; %>
<p>College: <%= college %></p>
2. Scriptlet Tag
Used to write Java statements (code executed inside service method).
Syntax
<% // Java code here %>
Example
<%
int a = 10;
int b = 20;
int sum = a + b;
%>
<p>Sum = <%= sum %></p>
3. Expression Tag
Outputs the value directly to browser.
Syntax
<%= expression %>
Example
<p>The value is: <%= 10 * 20 %></p>
JSP Action Tags
These are XML-style tags used to control runtime behavior.
Types:
<jsp:include>
<jsp:forward>
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:param>
<jsp:plugin>
1. <jsp:include>
Includes a file at run time.
Syntax
<jsp:include page="[Link]" />
Example
<jsp:include page="[Link]" />
2. <jsp:forward>
Forward request to another page.
Syntax
<jsp:forward page="[Link]" />
3. <jsp:useBean>
Create or access a JavaBean.
Syntax
<jsp:useBean id="obj" class="[Link]" scope="page|session|
application" />
<jsp:useBean id="s" class="[Link]" scope="session" />
4. <jsp:setProperty>
Set value to bean property.
Syntax
<jsp:setProperty name="beanId" property="propertyName"
value="value" />
Ex:
<jsp:setProperty name="u" property="name" value="John"/>
<jsp:setProperty name="s" property="age" value="20" />
5. <jsp:getProperty>
Retrieve bean value.
Syntax
<jsp:getProperty name="beanId" property="propertyName" />
Ex:
<p>Name: <jsp:getProperty name="s" property="name" /></p>
<p>Age: <jsp:getProperty name="s" property="age" /></p>
6.<jsp:plugin>
The <jsp:plugin> action tag is used to embed Java applets or
JavaBeans into a JSP page.
Syntax:
<jsp:plugin
type="applet"
code="ClassName"
codebase="url_path"
width="300"
height="300">
<jsp:params>
<jsp:param name="paramName" value="paramValue" />
</jsp:params>
</jsp:plugin>
Ex:
<html>
<head>
<title>Plugin Example</title>
</head>
<body>
<h3>Displaying Applet using jsp:plugin</h3>
<jsp:plugin
type="applet"
code="[Link]"
codebase="applets"
width="300"
height="200">
<jsp:params>
<jsp:param name="username" value="John" />
</jsp:params>
<jsp:fallback>
Applet not supported in your browser.
</jsp:fallback>
</jsp:plugin>
</body>
</html>
Difference Between include Directive and include Action
Point include Directive (< include Action
%@ include file="..." (<jsp:include
%>) page="..." />)
Type of Static inclusion Dynamic inclusion
Inclusion
Time of Translation time (before Request time (during page
Execution servlet compilation) execution)
How It Works Text of included file is Included resource is
merged with JSP before executed, and its output is
compilation added to response
Servlet Produces one combined Parent JSP and included
Generation servlet resource remain separate
servlets
Effect of File Requires JSP Changes appear
Changes recompilation to reflect immediately (no
changes recompilation needed)
Parameter ❌ Not supported ✅ Supported using
Passing <jsp:param>
Suitable For Static content (header, Dynamic content (DB
footer, navbar, templates) results, time-based output)
Performance Faster (no runtime Slightly slower (runtime
overhead) invocation)
Types of Mostly JSP/HTML Any resource (JSP, Servlet,
Resources fragments HTML, dynamic pages)
Syntax <%@ include <jsp:include
file="[Link]" %> page="[Link]"
flush="true" />
JavaBean Definition
A JavaBean is a reusable, software component written in Java that
follows specific conventions such as:
Must have a public no-argument constructor
Must provide getter and setter methods to access private properties
Must be serializable (optional in basic definitions)
JavaBeans are mainly used to store and transfer data between JSP,
Servlets, and other Java components.
Syntax:
public class ClassName implements [Link] {
// 1. Private properties
private DataType propertyName;
// 2. No-argument constructor
public ClassName() { }
// 3. Getter method
public DataType getPropertyName() {
return propertyName;
}
// 4. Setter method
public void setPropertyName(DataType propertyName) {
[Link] = propertyName;
}
}