Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

JSP and Servlets
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
  • The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
  • Servlet Request Parameters, and Forward vs Redirect
  • Run a Java Web App in Docker: Tomcat 11 With One Compose Command
  • Sessions, Cookies and Filters in Java Servlets: Log In Once, Stay Logged In
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
  • JavaBeans and JSP Scopes: Page, Request, Session and Application
  • JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
  • JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
  • JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
  • Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
  • MVC and the Front Controller With Servlets and JSP
  • Forms With JSP and Servlets: Handle the POST and Validate on the Server
  • Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
  • Users CRUD With Servlets and JSP, Part 2: Update and Delete
  1. Blog
  2. /
  3. JSP and Servlets
  4. /
  5. JSP

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026JSP

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 a String name and an int 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:

Left, the JSP tags. Right, the generated class. The declaration becomes a field outside _jspService, made once. The scriptlet is pasted inside _jspService and runs on every request. Plain HTML becomes out.write. The expression becomes out.print, which is why it takes no semicolon. counter.jsp (you write) counter_jsp.java (Tomcat writes) <%! int visits = 0; %> <% visits++; %> <h1>Visit number <%= visits %> private int visits = 0; _jspService(req, resp) { visits++; out.write("<h1>Visit..."); out.print(visits); field, once every request
A declaration goes outside _jspService; scriptlets, HTML and expressions go inside it.

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 into home.jsp before compiling, as just more out.write lines. 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 scriptletsServletTag-based JSP
Java livesinside the HTMLin a Java classin a Java class
HTML livesin the pagein out.printlnin the page
A loop isscriptlet tagsa plain fora 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 becomes out.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 import line. 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

TagNameBecomes
<%= x %>expressionout.print(x)
<% ... %>scriptletcode inside _jspService, every request
<%! ... %>declarationa field, made once
<%@ include %>directivefile pasted in at translation time
<jsp:include>actioncall to another page per request
<%-- --%>JSP commentnothing, removed on the server

Keep going in JSP and Servlets · 8 of 19

Previous
Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
Next post
JavaBeans and JSP Scopes: Page, Request, Session and Application
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Java inside an HTML page
  • A declaration: a field that keeps counting
  • What Jasper makes of each part
  • Import a class, loop in the page
  • Two comments, two audiences
  • Reuse a header and a footer
  • Translation time or request time
  • Where the Java belongs
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.