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. Servlets

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Servlets

What you will learn

  • Say what a servlet and a JSP are, and how they differ
  • Explain the jobs Tomcat does between an HTTP request and your code
  • See how Tomcat turns a JSP into a servlet class
  • Know what goes into a .war file and when to pick Tomcat or TomEE

Before you start

  • Basic Java
  • Basic HTML

A servlet is a Java class that answers a web request. A JSP is an HTML page with a little Java inside. They look very different, but on the server they are the same thing, and the piece that makes that true is the servlet container. In this course that container is Tomcat 11.

HTML with a little Java inside

Here is a complete JSP page, hello.jsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
<h1>Hello from JSP</h1>
<%-- the code between <%= and %> runs on the server --%>
<p>Tasks due today: <%= 2 + 1 %></p>
</body>
</html>

Open /hello.jsp and the browser shows:

Hello from JSP
Tasks due today: 3

Line 4, the <h1>, is plain HTML and goes out exactly as written. Line 6 contains <%= 2 + 1 %>, and that part runs on the server. The browser only ever sees 3.

Notice what's missing. There's no Java class and no @WebServlet, yet the page answers a URL. So where does the servlet come from?

A JSP becomes a servlet

On the first request for hello.jsp, Tomcat writes a Java file from it, hello_jsp.java, compiles it and runs it. Shortened, the generated class looks like this:

public final class hello_jsp extends HttpJspBase {
  public void _jspService(request, response) {
    response.setContentType("text/html;charset=UTF-8");
    out.write("<html>\n");
    out.write("<body>\n");
    out.write("<h1>Hello from JSP</h1>\n");
    out.write("\n");                        // the JSP comment is gone
    out.write("<p>Tasks due today: ");
    out.print( 2 + 1 );                     // your Java, pasted in
    out.write("</p>\n");
    out.write("</body>\n");
    out.write("</html>\n");
  }
}

Each piece of your page maps to one line:

  • The page contentType directive becomes response.setContentType(...).
  • Every HTML tag becomes an out.write(...).
  • The JSP comment <%-- ... --%> disappears. It never reaches the browser.
  • The expression 2 + 1 is pasted into out.print(...).

So a JSP is a servlet that you write as HTML. Every JSP turns into a servlet before it runs, and Tomcat only does that translation once.

What Tomcat does for you

An HTTP request arrives as plain text, something like GET /hello.jsp HTTP/1.1 plus a Host header. Tomcat sits between that text and your code and does five jobs:

  1. It listens on a port (8080 by default) and reads the request text.
  2. It builds a request object and a response object that your Java code can use.
  3. It finds the servlet for the URL.
  4. If the URL is a JSP, it turns the JSP into a servlet, once.
  5. It calls the servlet, then sends the answer back as an HTTP response, for example HTTP/1.1 200 with Content-Type: text/html;charset=UTF-8.
An HTTP request as text goes into Tomcat 11, which listens on port 8080, builds request and response objects, finds the servlet for the URL, turns a JSP into a servlet once, and calls it. An HTTP response as text comes out. HTTP request GET /hello.jsp plain text Tomcat 11 HTTP response 200, text/html plain text 1 listens on port 8080, reads the text 2 builds request and response objects 3 finds the servlet for the URL 4 turns a JSP into a servlet, once 5 calls it, sends the answer back
You write the servlet or the JSP. The container handles the networking, the objects and the lifecycle.

What goes into a .war

Your code reaches Tomcat as a .war file: one archive you hand to the server. For this small project, jsp-hello.war holds:

  • the JSP pages, hello.jsp and index.jsp
  • the configuration, WEB-INF/web.xml
  • your compiled servlet classes, under WEB-INF/classes

Tomcat unpacks the war, maps URLs to servlets, compiles each JSP and answers every request.

Same Tomcat, two packages

You'll meet two names, Tomcat and TomEE.

  • Tomcat 11 is a servlet container. It gives you Servlet 6.1, JSP 4.0, Expression Language 6.0 and WebSocket 2.2. The package name is jakarta.servlet (Tomcat 9 and older used javax.servlet). It needs Java 17 or later; this course uses Java 25.
  • TomEE is Tomcat plus more of Jakarta EE: CDI for injecting objects, JPA for the database, JAX-RS for REST, Bean Validation, JTA transactions and JSF. TomEE 11 runs on Tomcat 11.

Pick Tomcat when you need servlets and JSP; it is smaller and lighter. Pick TomEE when the app is plain Jakarta EE with no Spring, and every extra comes from the server. This part of the course uses plain Tomcat 11. Either way, the servlet engine inside is the same.

Servlet or JSP?

ServletJSP
You writea Java classan HTML page with a little Java
Outputout.println("<h1>...")<h1>...</h1> as plain HTML
Tomcat runs it asa servleta generated servlet
Best athandling the requestshowing the page

Use a servlet to decide what happens and a JSP to show the result. And for a page with nothing to compute, plain HTML works fine.

Common mistakes

  • Thinking the browser runs the Java in a JSP. It only receives the output, the 3, never the expression.
  • Mixing javax.servlet imports from old tutorials into a Tomcat 11 project. Tomcat 10 and later use jakarta.servlet.
  • Using an HTML comment <!-- --> for a private note. That one is sent to the browser. A JSP comment <%-- --%> is removed by Tomcat, as the generated class shows.

Quick recap

TermMeaning
Servlet containerTomcat: turns HTTP text into Java calls and back
ServletA Java class that handles a request
JSPHTML with a little Java, compiled into a servlet on first use
.warOne archive with pages, servlet classes and web.xml

Next, see how a servlet answers a URL and lives through init, doGet and destroy in The Servlet Request Lifecycle.

Keep going in JSP and Servlets · 1 of 19

Next post
Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
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

Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA

Set JAVA_HOME, install Tomcat 11 on a free port, create a Jakarta EE web application with Maven in IntelliJ IDEA, read its folders and pom.xml, and run it on Tomcat.

The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy

A servlet is a Java class that answers a URL. Follow one request through Tomcat, map servlets with @WebServlet and web.xml, build a page from a list, and watch init, doGet and destroy run.

Servlet Request Parameters, and Forward vs Redirect

Read ?name=Emma with getParameter, then hand a request on two ways: a forward keeps one request and its attributes, a redirect makes the browser ask again and the attribute is gone.

Run a Java Web App in Docker: Tomcat 11 With One Compose Command

Run a servlet project without installing Tomcat: one docker compose command builds the WAR with Maven on Java 25, starts Tomcat 11 in a container, and serves the app on localhost:8090.

On this page

  • HTML with a little Java inside
  • A JSP becomes a servlet
  • What Tomcat does for you
  • What goes into a .war
  • Same Tomcat, two packages
  • Servlet or JSP?
  • 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.