
Play Store Application link – Advance java – in 15 steps – Apps on Google Play
Learn with our youtube video –
Directive | Purpose | Syntax |
---|---|---|
page | Sets various attributes for the JSP page, such as content type, error page, session tracking, etc. | <%@ page attribute1=”value1″ attribute2=”value2″ %> |
include | Includes the content of another file in the current JSP file | <%@ include file="filename" %> |
taglib | Includes a tag library in the JSP page | <%@ taglib uri="taglibrary-uri" prefix="tag-prefix" %> |
1- page directives

Directives | Description |
---|---|
<%@ page language="java" %> | Specifies the programming language used in the JSP page. |
<%@ page import="java.util.*" %> | Imports classes from the Java package. |
<%@ page contentType="text/html" %> | Specifies the MIME type of the response. |
<%@ page session="true" %> | Indicates whether the page participates in a session. |
<%@ page buffer="8kb" %> *Deprecated | Specifies the buffer size for the response. |
<%@ page errorPage="error.jsp" %> | Specifies the error page to which the client is redirected if an error occurs. |
<%@ page isErrorPage="true" %> | Indicates that the current page is an error page. |
<%@ page extends="package.class" %> *Deprecated | *(Reserved)Not recommended to use* Specifies the superclass for the generated servlet. |
<%@ page info="info" %> | Provides a string that can be accessed with the ServletInfo interface. |
<%@ page isThreadSafe="true" %> *Deprecated | Indicates whether the generated servlet can handle multiple requests simultaneously. |
<%@ page pageEncoding="UTF-8" %> *Deprecated | Specifies the character encoding for the JSP page. |
<%@ page autoFlush="true" %> *Deprecated | Indicates whether the buffer should be automatically flushed when it is full. |
<%@ page trimDirectiveWhitespaces="true" %> *Deprecated | Determines whether whitespace should be removed from template text in a JSP page. |
<%@ page deferredSyntaxAllowedAsLiteral="true" %> *Deprecated | *Removed in new version of jsp* Allows the use of expressions that are deferred until the time they are used. |
<%@ page isELIgnored="true/false" %> | Indicates whether or not the Expression Language is ignored. |
2- include directives
JSP include directive is used to include the contents of another file to the current JSP page. The included file can be HTML, JSP, text files etc.
<%@ include file=”test.html” %>
3- taglib directives
Important Topic (Requisite) –
It’s used to simplify the process of accessing data.
Syntax- ${expression}
Here is an example of how to use the Expression Language (EL) in a JSP page:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>EL Example</title>
</head>
<body>
<%
// Setting a variable in the page scope
request.setAttribute("name", "John Smith");
%>
<h1>Hello, ${name}</h1>
<p>Today's date is ${today}</p>
</body>
</html>
You can also use EL for simple mathematical and logical operations, for example:
<p>The result of 2 + 2 is ${2+2}</p>
The result of this will be
The result of 2 + 2 is 4
Now we have 3 types of tags in JSP.
A- Predefined External Tags – JSTL (JavaServer Pages Standard Tag Library)-
B- Internal Tags – JSP Action Tags
C- Custom Tags
A- Predefined External Tags – JSTL (JavaServer Pages Standard Tag Library)-
it’s a standard library of ready made tags.
To use these tags in your project, you need to setup it’s jar in the project, using following ways.
(i)- Download jar file and add into project – https://repo1.maven.org/maven2/jstl/jstl/1.2/jstl-1.2.jar
(ii)- Using Maven (pom.xml) –
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Classification of jstl-
1- Core Tags
2- Formatting Tags
3- Sql Tags
4- Xml tags
5- Jstl functions
Tag Name | Usage Scenarios | Code Snippet |
---|---|---|
Core Tags | General-purpose tags for manipulating data | <c:set var="name" value="${user.name}" /> |
Formatting Tags | Formatting data (dates, numbers, etc.) | <fmt:formatDate value="${order.date}" pattern="MM/dd/yyyy" /> |
SQL Tags | Executing SQL queries | <sql:query dataSource="${dataSource}" var="result"> SELECT * FROM users </sql:query> |
XML Tags | Manipulating XML documents | <x:parse xml="${xmlData}" var="parsedXml" /> |
JSTL Functions | Additional utility functions | ${fn:length(stringVariable)} |
1- Core tags : Code Example
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>JSTL Core Tags Example</title>
</head>
<body>
<h1>JSTL Core Tags Example</h1>
<c:set var="name" value="Kuldeep kaushik" />
<c:if test="${not empty name}">
<p>Welcome, <c:out value="${name}" /></p>
</c:if>
<c:choose>
<c:when test="${empty name}">
<p>No name provided.</p>
</c:when>
<c:otherwise>
<p>Your name is <c:out value="${name}" /></p>
</c:otherwise>
</c:choose>
<c:forEach var="i" begin="1" end="5" step="1">
<p>Iteration: <c:out value="${i}" /></p>
</c:forEach>
<c:set var="fruits" value="Apple, Banana, Orange" />
<ul>
<c:forEach items="${fruits}" var="fruit">
<li><c:out value="${fruit}" /></li>
</c:forEach>
</ul>
<c:url var="googleUrl" value="http://www.google.com" />
<p><a href="<c:out value="${googleUrl}" />">Go to Google</a></p>
<%-- <c:redirect url="${googleUrl}" /> --%>
</body>
</html>
2- Formatting tags: Code Example
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>JSTL Formatting Tags Example</title>
</head>
<body>
<h1>JSTL Formatting Tags Example</h1>
<fmt:formatNumber value="12345.6789" pattern="#,##0.00" var="formattedNumber" />
<p>Formatted Number: <fmt:out value="${formattedNumber}" /></p>
<fmt:parseNumber var="parsedNumber" value="12,345.6789" />
<p>Parsed Number: <fmt:out value="${parsedNumber}" /></p>
<fmt:formatDate value="2023-07-04" pattern="dd/MM/yyyy" var="formattedDate" />
<p>Formatted Date: <fmt:out value="${formattedDate}" /></p>
<fmt:parseDate var="parsedDate" value="04/07/2023" pattern="dd/MM/yyyy" />
<p>Parsed Date: <fmt:out value="${parsedDate}" /></p>
<fmt:message key="greeting.message" var="message" />
<p>Message: <fmt:out value="${message}" /></p>
</body>
</html>
3- SQL Tags
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html>
<html>
<head>
<title>JSTL SQL Tags Example</title>
</head>
<body>
<h1>JSTL SQL Tags Example</h1>
<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mydatabase" user="username" password="password" />
<sql:update dataSource="${dataSource}">
CREATE TABLE IF NOT EXISTS employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT
)
</sql:update>
<sql:update dataSource="${dataSource}">
INSERT INTO employees (name, age)
VALUES ('John Doe', 30)
</sql:update>
<sql:query dataSource="${dataSource}" var="result">
SELECT * FROM employees
</sql:query>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<c:forEach items="${result.rows}" var="row">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.name}" /></td>
<td><c:out value="${row.age}" /></td>
</tr>
</c:forEach>
</table>
<sql:transaction dataSource="${dataSource}" autoCommit="true">
<sql:update>
DELETE FROM employees WHERE age > 40
</sql:update>
</sql:transaction>
</body>
</html>
4- XML tags
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<!DOCTYPE html>
<html>
<head>
<title>JSTL XML Tags Example</title>
</head>
<body>
<h1>JSTL XML Tags Example</h1>
<x:parse doc="${xmlDocument}" var="parsedXml">
<x:xml>
<employees>
<employee>
<id>1</id>
<name>John Doe</name>
<age>30</age>
</employee>
<employee>
<id>2</id>
<name>Jane Smith</name>
<age>35</age>
</employee>
</employees>
</x:xml>
</x:parse>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<x:forEach select="$parsedXml/employees/employee" var="employee">
<tr>
<td><x:out select="$employee/id" /></td>
<td><x:out select="$employee/name" /></td>
<td><x:out select="$employee/age" /></td>
</tr>
</x:forEach>
</table>
<x:transform xml="${xmlDocument}" xslt="${xsltDocument}" var="transformedXml" />
<h2>Transformed XML:</h2>
<pre><x:out select="$transformedXml" /></pre>
</body>
</html>
5- JSTL Functions
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<title>JSTL Functions Example</title>
</head>
<body>
<h1>JSTL Functions Example</h1>
<p>Current Date: ${fn:substringAfterLast('2023-07-04', '-')}</p>
<p>Length of 'OpenAI': ${fn:length('OpenAI')}</p>
<p>Concatenated Strings: ${fn:concat('Hello', ' ', 'World')}</p>
<p>Reversed String: ${fn:reverse('Hello World')}</p>
<p>Upper Case: ${fn:toUpperCase('hello world')}</p>
<p>Lower Case: ${fn:toLowerCase('HELLO WORLD')}</p>
<p>Substring: ${fn:substring('Hello World', 6, 11)}</p>
<p>Replace: ${fn:replace('Hello World', 'World', 'Universe')}</p>
<p>Contains: ${fn:contains('Hello World', 'World')}</p>
<p>Trim: ${fn:trim(' Hello World ')}</p>
<p>Escape XML: ${fn:escapeXml('<tag>')}</p>
</body>
</html>
B- Internal Tags – JSP Action Tags
Action Tag | Description | Example |
---|---|---|
<jsp:include> | Includes the content of another resource within the current JSP page. | <jsp:include page="header.jsp" /> |
<jsp:forward> | Forwards the request to another resource. | <jsp:forward page="login.jsp" /> |
<jsp:param> *Deprecated | Adds a parameter to the request when used in conjunction with <jsp:include> or <jsp:forward> . | <jsp:param name="id" value="123" /> |
<jsp:useBean> | Finds or instantiates a JavaBean and makes it available to the JSP page. | <jsp:useBean id="user" scope="session" class="com.example.User" /> |
<jsp:setProperty> | Sets the property of a JavaBean. | <jsp:setProperty name="user" property="username" value="johndoe" /> |
<jsp:getProperty> | Gets the property of a JavaBean and prints it to the response. | <jsp:getProperty name="user" property="username" /> |
<jsp:plugin> *Deprecated | Generates HTML and JavaScript to make sure that a browser has the Java plugin installed and directs the browser to download the plugin if necessary. | <jsp:plugin type="applet" code="MyApplet.class" width="200" height="100" /> |
<jsp:element> *Deprecated | Defines a XML element and its content. | <jsp:element name="customer" > <jsp:attribute name="id" >123</jsp:attribute> John Doe </jsp:element> |
<jsp:attribute> *Deprecated | Defines a XML attribute | <jsp:attribute name="id" >123</jsp:attribute> |
<jsp:body> *Deprecated | Defines the body content of a XML element | <jsp:element name="customer"> <jsp:attribute name="id" >123</jsp:attribute> <jsp:body>John Doe</jsp:body> </jsp:element> |
<jsp:fallback> *Deprecated | Defines a fallback mechanism for browsers that don’t support the plugin | <jsp:fallback>Sorry, your browser doesn't support the required plugin. </jsp:fallback> |
<jsp:output> *Deprecated | Defines the version and encoding of the JSP page | <jsp:output doctype-root-element="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" doctype-system="http://www.w3.org/TR/html4/loose.dtd" version="1.0" encoding="UTF-8" /> |
<jsp:scriptlet> | Alternative for simple jsp scriplet tags (<% %>) | <jsp:scriptlet> int sum = 2 + 3; out.println("Sum: " + sum); </jsp:scriptlet> |
JSP Action Tags : Code Example
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP Action Tags Example</title>
</head>
<body>
<h1>JSP Action Tags Example</h1>
<jsp:useBean id="person" class="Person" scope="session" />
<jsp:setProperty name="person" property="name" value="Kuldeep" />
<jsp:setProperty name="person" property="age" value="30" />
<p>Person Name: <jsp:getProperty name="person" property="name" /></p>
<p>Person Age: <jsp:getProperty name="person" property="age" /></p>
<jsp:include page="included.jsp" />
<jsp:forward page="forwarded.jsp" />
<jsp:scriptlet>
int sum = 2 + 3;
out.println("Sum: " + sum);
</jsp:scriptlet>
<%-- <jsp:element name="div" id="myDiv">
<jsp:attribute name="class" value="my-class">
</jsp:attribute>
<jsp:body>
This is the content of the div element.
</jsp:body>
</jsp:element> --%>
<%-- <jsp:plugin type="application/x-shockwave-flash" codebase="http://www.youtube.com/v/" width="640" height="480" code="asd">
<jsp:param name="movie" value="VIDEO_ID_HERE" />
</jsp:plugin> --%>
<%-- <jsp:fallback> </jsp:fallback> --%>
<%-- <jsp:output doctype-root-element="html"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
omit-xml-declaration="true" /> --%>
</body>
</html>
where Person and Applet class are as following –
package com.example;
public class Person {
private String name;
private int age;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.example;
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
private String param1;
private String param2;
@Override
public void init() {
param1 = getParameter("param1");
param2 = getParameter("param2");
}
@Override
public void paint(Graphics g) {
g.drawString("Parameter 1: " + param1, 10, 10);
g.drawString("Parameter 2: " + param2, 10, 30);
}
}
C- Custom Tag :
(1) greetings.tag in WEB-INF folder
<%@ attribute name="name" %>
Hello, ${name}!
(2) JSP file in webapp folder
<%@ taglib prefix="mytag" tagdir="/WEB-INF/" %>
<html>
<body>
<mytag:greetings name="Kuldeep"/>
</body>
</html>