Understanding JSP Tags: A Comprehensive Guide

Karikaranvetti
4 min readJan 22, 2024

--

JavaServer Pages (JSP) provide a dynamic and efficient way to create web applications by seamlessly integrating Java code into HTML. At the heart of JSP’s versatility are its tags, each serving a distinct purpose in crafting dynamic and feature-rich web pages. In this comprehensive guide, we will delve into various JSP tags, exploring their functionalities through small examples. To illustrate their collective power, we’ll conclude with a real-world example of an e-commerce product page.

Table of Contents

  1. Introduction to JSP Tags
  2. Declaration Tags (<%! %>) - Defining Variables and Methods
  3. Scriptlet Tags (<% %>) - Embedding Java Code
  4. Expression Tags (<%= %>) - Outputting Dynamic Content
  5. Directive Tags (<%@ %>) - Global Page Settings
  6. Include Directive (<%@ include %>) - Reusable Content
  7. Action Tags (e.g., <jsp:include>) - Dynamic Page Control
  8. Comment Tags (<%-- --%>) - Adding Comments
  9. Real-World Example: E-commerce Product Page
  10. Conclusion

Introduction to JSP Tags

JavaServer Pages (JSP) tags are special constructs embedded within JSP pages to include Java code, control page flow, and manage dynamic content generation. Tags provide a way to seamlessly integrate server-side logic into the HTML structure, making it easier to create dynamic and data-driven web applications.

Declaration Tags (<%! %>) - Defining Variables and Methods

Purpose: Declaration tags are used to declare variables and methods that persist beyond the service method. They provide a way to define instance variables and methods that are outside the main body of the generated servlet.

Example:

<%! 
int visitCount = 0;
void incrementVisitCount() {
visitCount++;
}
%>

In this example, we declare an integer variable visitCount and a method incrementVisitCount that increments the count.

Scriptlet Tags (<% %>) - Embedding Java Code

Purpose: Scriptlet tags allow the embedding of Java code directly into the HTML content of the JSP page. The code within scriptlet tags is executed each time the page is requested.

Example:

<%
String name = "John";
out.println("Hello, " + name);
%>

Here, we define a String variable name and use it to print a personalized greeting.

Expression Tags (<%= %>) - Outputting Dynamic Content

Purpose: Expression tags are used to evaluate and output expressions directly into the HTML content. They provide a shorthand for printing the result using out.print().

Example:

<p>Welcome, <%= name %>!</p>
<p>Visit Count: <%= visitCount %></p>

These tags allow us to dynamically insert values like the user’s name and the visit count directly into the HTML This is implicitly using Out variable .

Directive Tags (<%@ %>) - Global Page Settings

Purpose: Directive tags are used to provide global information about the entire JSP page. They include settings such as page language, imports, content type, and more.

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

This directive sets the page’s language and character encoding.

Include Directive (<%@ include %>) - Reusable Content

Purpose: Include directive is used to include the content of another file during the translation phase. This promotes code reuse and modularity.

Example:

<%@ include file="header.jsp" %>

This includes the content of the header.jsp file in the current JSP page.

Action Tags (e.g., <jsp:include>) - Dynamic Page Control

Purpose: Action tags provide a dynamic way to control page flow. For example, <jsp:include> is used to dynamically include the content of another resource.

Example:

<jsp:include page="footer.jsp" />

This tag dynamically includes the content of footer.jsp into the current page.

Comment Tags (<%-- --%>) - Adding Comments

Purpose: Comment tags are used for adding comments in the JSP page. The content within comment tags is not displayed in the final HTML output.

Example:

<%-- This is a comment --%>

Comments help document the code and are not visible in the rendered HTML.

Real-World Example: E-commerce Product Page

Imagine you are building an e-commerce product page using JSP. In this scenario, we can utilize various JSP tags to create a dynamic and modular web page.

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product Details</title>
</head>
<body>

<!-- Declaration Tags -->
<%!
String productName = "Smartphone";
double productPrice = 599.99;
int productQuantity = 100;
%>

<!-- Scriptlet Tags -->
<%
String username = (String) session.getAttribute("username");
%>

<!-- Expression Tags -->
<h1>Welcome, <%= username %>!</h1>
<h2><%= productName %> Details</h2>
<p>Price: $<%= productPrice %></p>
<p>Available Quantity: <%= productQuantity %></p>

<!-- Directive Tags -->
<%@ page import="com.example.Product" %>
<%@ page isErrorPage="false" %>

<!-- Include Directive -->
<%@ include file="header.jsp" %>

<!-- Action Tags -->
<jsp:include page="related_products.jsp" />

<!-- Comment Tags -->
<%--
This section contains additional product details and recommendations.
--%>

<!-- Your HTML Content Goes Here -->

<!-- Footer Include -->
<jsp:include page="footer.jsp" />

</body>
</html>

In this real-world example:

  • Declaration Tags: Declare product details as instance variables.
  • Scriptlet Tags: Retrieve the username from the session.
  • Expression Tags: Display dynamic content such as the username, product details, and quantity.
  • Directive Tags: Import a custom Product class and set the isErrorPage attribute.
  • Include Directive: Include the header and footer for consistency.
  • Action Tags: Dynamically include related products using <jsp:include>.
  • Comment Tags: Add comments for documentation purposes.

This example showcases how JSP tags can be utilized to build a dynamic and modular e-commerce product page.

--

--

Karikaranvetti
Karikaranvetti

Written by Karikaranvetti

Software Engineer @Enactor | B.Sc. Eng(Hons) at University of Peradeniya

No responses yet