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.
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 contentTypedirective becomesresponse.setContentType(...). - Every HTML tag becomes an
out.write(...). - The JSP comment
<%-- ... --%>disappears. It never reaches the browser. - The expression
2 + 1is pasted intoout.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:
- It listens on a port (8080 by default) and reads the request text.
- It builds a request object and a response object that your Java code can use.
- It finds the servlet for the URL.
- If the URL is a JSP, it turns the JSP into a servlet, once.
- It calls the servlet, then sends the answer back as an HTTP response, for example
HTTP/1.1 200withContent-Type: text/html;charset=UTF-8.
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.jspandindex.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 usedjavax.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?
| Servlet | JSP | |
|---|---|---|
| You write | a Java class | an HTML page with a little Java |
| Output | out.println("<h1>...") | <h1>...</h1> as plain HTML |
| Tomcat runs it as | a servlet | a generated servlet |
| Best at | handling the request | showing 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.servletimports from old tutorials into a Tomcat 11 project. Tomcat 10 and later usejakarta.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
| Term | Meaning |
|---|---|
| Servlet container | Tomcat: turns HTTP text into Java calls and back |
| Servlet | A Java class that handles a request |
| JSP | HTML with a little Java, compiled into a servlet on first use |
| .war | One 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.
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
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.
