Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

JSP: Scripting Elements, Directives, Standard Actions, and Custom Tag Libraries

Lesson 46 of 46 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

JSP: Scripting Elements, Directives, Standard Actions, and Custom Tag Libraries

JSP Scripting Elements

JSP provides three tags for embedding Java logic directly in a page:

1. Declaration <%! %>

Declares methods or fields at the class level of the generated servlet (outside _jspService).

<%!
    private int counter = 0;
    private String greet(String name) {
        return "Hello, " + name + "!";
    }
%>

2. Scriptlet <% %>

Embeds a block of Java statements, executed on every request inside _jspService.

<%
    int total = 0;
    for (int i = 1; i <= 5; i++) {
        total += i;
    }
%>
<p>Total: <%= total %></p>

3. Expression <%= %>

Evaluates a Java expression and inserts the result directly into the output (no semicolon).

<p>Current time: <%= new java.util.Date() %></p>
<p>Counter value: <%= counter %></p>
TagSyntaxBecomes (in generated servlet)
Declaration<%! ... %>Class-level field/method
Scriptlet<% ... %>Statements inside _jspService
Expression<%= ... %>Argument to out.print(...)
Comment<%-- ... --%>Removed entirely (not sent to browser)

JSP Directives

Directives give the container page-level instructions at translation time. They don't produce output.

<%-- page directive: page-wide settings --%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         import="java.util.*, java.text.SimpleDateFormat"
         errorPage="error.jsp" %>

<%-- include directive: static include at translation time --%>
<%@ include file="header.jsp" %>

<%-- taglib directive: import a custom/standard tag library --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
DirectivePurpose
pagePage settings — content type, imports, error page, session usage
includeTextually merges another file at translation time
taglibDeclares a custom or standard tag library and its prefix

JSP Standard Actions

Standard actions are XML-like tags (<jsp:...>) that perform predefined tasks at request time (unlike the include directive, which runs at translation time).

<%-- Include another resource's output at request time --%>
<jsp:include page="footer.jsp" />

<%-- Forward the request to another resource --%>
<jsp:forward page="welcome.jsp" />

<%-- Instantiate or reuse a JavaBean --%>
<jsp:useBean id="student" class="com.example.Student" scope="session" />

<%-- Set a bean property --%>
<jsp:setProperty name="student" property="name" value="Alice" />

<%-- Read a bean property --%>
<jsp:getProperty name="student" property="name" />
ActionPurpose
<jsp:include>Includes another resource's output at request time
<jsp:forward>Forwards the request to another resource, ending current processing
<jsp:useBean>Creates/locates a JavaBean instance in a given scope
<jsp:setProperty>Sets a property on a bean (often from request parameters)
<jsp:getProperty>Outputs the value of a bean property

include Directive vs jsp:include Action

Feature<%@ include %> (directive)<jsp:include> (action)
WhenTranslation time (static)Request time (dynamic)
ResultContent merged into one servletSeparate resource invoked, output inserted
PerformanceFaster (compiled once)Slight overhead per request
Use caseStatic headers/footersContent that changes per request

Custom Tag Libraries

Custom tags let you encapsulate reusable presentation logic as XML-like tags, keeping Java code out of JSP pages entirely.

1. Tag Handler Class

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.IOException;

public class HelloTag extends TagSupport {
    private String name;

    public void setName(String name) { this.name = name; }

    @Override
    public int doStartTag() throws JspException {
        try {
            pageContext.getOut().print("<b>Hello, " + name + "!</b>");
        } catch (IOException e) {
            throw new JspException(e);
        }
        return SKIP_BODY;
    }
}

2. Tag Library Descriptor (mytags.tld)

<taglib>
    <tlib-version>1.0</tlib-version>
    <short-name>my</short-name>
    <tag>
        <name>hello</name>
        <tag-class>com.example.HelloTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
        </attribute>
    </tag>
</taglib>

3. Using the Custom Tag in a JSP

<%@ taglib uri="/WEB-INF/mytags.tld" prefix="my" %>

<my:hello name="Alice" />
Key Takeaway: Scripting elements (<%! %>, <% %>, <%= %>) embed Java in a JSP page, directives (page, include, taglib) configure the page at translation time, and standard actions (jsp:include, jsp:forward, jsp:useBean) perform dynamic, request-time tasks. Custom tag libraries push this further — encapsulating reusable logic behind clean, HTML-like tags so JSP pages stay free of embedded Java.