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>
| Tag | Syntax | Becomes (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" %>
| Directive | Purpose |
|---|---|
page | Page settings — content type, imports, error page, session usage |
include | Textually merges another file at translation time |
taglib | Declares 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" />
| Action | Purpose |
|---|---|
<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) |
|---|---|---|
| When | Translation time (static) | Request time (dynamic) |
| Result | Content merged into one servlet | Separate resource invoked, output inserted |
| Performance | Faster (compiled once) | Slight overhead per request |
| Use case | Static headers/footers | Content 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.