JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
The JSP tags you'll read in older Java web apps: expressions, scriptlets, declarations, page imports, two kinds of comment, and the include directive versus the jsp:include action, each shown with what Tomcat turns it into.
What you will learn
- Print values with an expression and run Java with a scriptlet
- Use a declaration to make a field that lives across requests
- Tell an HTML comment from a JSP comment and know which one the browser sees
- Reuse a header and footer with the include directive and the jsp:include action
Before you start
- Servlets and JSP: what Tomcat does
- Basic HTML
Most business logic belongs in a servlet. Still, sometimes you want a little logic right on the page, and that's what JSP lets you do. This article covers the tags you'll meet: expressions, scriptlets and declarations, then imports, comments and includes. For each one we'll see what Tomcat turns it into, because that explains how it behaves.
Java inside an HTML page
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
<%-- scriptlet: Java statements, run on every request --%>
<%
String name = "Emma";
int tasks = 2 + 1;
%>
<%-- expression: its value is printed into the page --%>
<h1>Hello, <%= name %></h1>
<p>Tasks due today: <%= tasks %></p>
</body>
</html>
Hello, Emma
Tasks due today: 3
<%@ ... %>is a directive: settings for the page, here the content type.<% ... %>is a scriptlet: ordinary Java statements, as many lines as you like. Here it makes aString nameand anint tasks.<%= ... %>is an expression: its value is printed into the page. Give it a value or a variable, with no semicolon.
A declaration: a field that keeps counting
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- declaration: a field of the class, made once --%>
<%! private int visits = 0; %>
<%-- scriptlet: runs on every request --%>
<% visits++; %>
<html>
<body>
<h1>Visit number <%= visits %></h1>
</body>
</html>
Open counter.jsp three times:
Visit number 1
Visit number 2
Visit number 3
<%! ... %> is a declaration. private int visits becomes a field of the generated class, created once. The scriptlet visits++ runs on every request, and the expression prints the value. The third visit shows 3 because there is one page object, just like there is one servlet object.
What Jasper makes of each part
Tomcat's JSP engine, Jasper, converts your JSP into a Java class with a method called _jspService. Where each tag lands decides how it behaves:
You don't have to remember the tags; an AI assistant can write them. But you do need to know what each one becomes, or the counter above will surprise you.
Import a class, loop in the page
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- import works like a Java import, for the code in this page --%>
<%@ page import="java.util.List, java.util.ArrayList" %>
<%
List<String> tasks = new ArrayList<>();
tasks.add("Write the report");
tasks.add("Email Olivia");
tasks.add("Book the meeting room");
%>
<html>
<body>
<h1>Today's tasks</h1>
<!-- HTML comment: sent to the browser -->
<%-- JSP comment: removed on the server --%>
<ul>
<% for (String task : tasks) { %>
<li><%= task %></li>
<% } %>
</ul>
</body>
</html>
Today's tasks
* Write the report
* Email Olivia
* Book the meeting room
<%@ page import="..." %> works like a normal Java import, for the code in this page. Then look at the loop: one Java for loop is split across three tags. The for opens in a scriptlet, an expression prints each task, and the closing brace sits in its own scriptlet. Java and HTML are woven together.
There are three kinds of directive, page, include and taglib, and all of them start with <%@.
Two comments, two audiences
tasks.jsp has both kinds of comment. Run it, right-click the page and choose View page source. The source contains <!-- HTML comment: sent to the browser -->, and there is no trace of the JSP comment. The JSP comment is removed on the server and never leaves it. So anything private goes in a <%-- --%> comment, never in an HTML comment.
Reuse a header and a footer
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<%-- not a JSP: Tomcat sends style.css as it is --%>
<link rel="stylesheet" href="style.css">
</head>
<body>
<%-- directive: header.jsp is copied in when the page is translated --%>
<%@ include file="header.jsp" %>
<p>Welcome back, Liam.</p>
<%-- action: footer.jsp runs as its own page, on every request --%>
<jsp:include page="footer.jsp" />
</body>
</html>
The two included files are four lines each:
<%-- copied in by the include directive --%>
<header>
<h1>Riverside Book Club</h1>
</header>
<%-- run by jsp:include, on every request --%>
<footer>
<p>Open Monday to Friday</p>
</footer>
The page shows:
Riverside Book Club (white on a blue bar)
Welcome back, Liam.
Open Monday to Friday (grey, under a thin line)
The <link> to style.css is plain HTML. The browser asks Tomcat for that file, and since it isn't a JSP, Tomcat sends it as it is.
Translation time or request time
The two includes look alike but work differently:
<%@ include file="header.jsp" %>is a directive. The header is pasted intohome.jspbefore compiling, as just moreout.writelines. That happens once, at translation time.<jsp:include page="footer.jsp" />is an action. It's a call to a separate page, made on every request.
You can see it in Tomcat's work folder: there's a home_jsp and a footer_jsp, but no header_jsp, because the header lives inside home. A plain common header or footer just needs the directive. If the included part has its own logic to run per request, the action is the better fit.
Where the Java belongs
| JSP with scriptlets | Servlet | Tag-based JSP | |
|---|---|---|---|
| Java lives | inside the HTML | in a Java class | in a Java class |
| HTML lives | in the page | in out.println | in the page |
| A loop is | scriptlet tags | a plain for | a tag, no Java |
Mixing two languages in one file is hard to read and debug. Scriptlets are fine for a small page such as an admin panel, but as a rule, keep the business logic in a servlet and use the JSP for display. The tag-based way is covered in JSTL core tags.
Common mistakes
- A semicolon inside an expression,
<%= name; %>. The page fails with HTTP 500:Syntax error on token ";", delete this token. It becomesout.print(name;). <% int visits = 0; %>instead of a declaration. Now it's a local variable inside_jspService, and every visit says "Visit number 1".- Deleting the
page importline. HTTP 500:List cannot be resolved to a type. - A typo in an include,
file="heder.jsp". HTTP 500:JSP file [heder.jsp] not found.
Quick recap
| Tag | Name | Becomes |
|---|---|---|
<%= x %> | expression | out.print(x) |
<% ... %> | scriptlet | code inside _jspService, every request |
<%! ... %> | declaration | a field, made once |
<%@ include %> | directive | file pasted in at translation time |
<jsp:include> | action | call to another page per request |
<%-- --%> | JSP comment | nothing, removed on the server |
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
A JSP looks like HTML with a little Java, yet Tomcat runs it as a servlet. See what a servlet container does for each request, what the generated hello_jsp.java looks like, what a .war holds, and Tomcat vs TomEE.
JavaBeans and JSP Scopes: Page, Request, Session and Application
A Student bean filled from a form with jsp:useBean and setProperty, then one Counter class kept in four scopes. Three visits, two browsers and an include page show exactly who sees which number.
JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
Expression Language prints a value in a JSP with ${...} and no Java on the page. Read a Student bean, a price map, a tag list and ?city=Boston, run small checks, and see the order EL searches the scopes.
JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
Five JSTL core tags let a JSP decide and repeat with no Java on the page. Add the jars to pom.xml, then use c:set, c:out (and see why it is safer than plain EL), c:if, c:choose and c:forEach on three orders.
