JSP/Servlet | Servlet | Difference Between Servlets and JSP
Besides JSP, there is another technology that can be called a foundation of server-side Java. It is the Servlet. How exactly are these two different?
The answer is that they are the same. In other words, JSP and servlets are the same thing. More precisely, there is no such thing as a separate server-side scripting language called JSP. JSP is actually a servlet.
Let’s organize this. Server-side Java programs are very different from ordinary applications. Applications are launched and executed directly, but server-side Java programs are not.
On the server side, there is a Java server, and programs are developed to run inside it. You can think of it as similar to including and running a small program called an applet in a web browser. An applet is not an application. There is a framework prepared in advance for running small programs, and when a program built for that framework is embedded in a web page, it is automatically recognized and runs.
The server side is similar. A Java server has a structure prepared for running programs inside it. Programs are created according to that structure and included there. When a user accesses the URL assigned to that program, the Java server runs the program.
This “small program that runs in a Java server” is a servlet. Think of replacing “web page and applet” with “Java server and servlet”.
Then what is JSP? Since a servlet is ultimately a Java program, creating one is somewhat difficult. Everything must be coded in Java. Not only the internal processing, but also all HTML code displayed to the client, that is, the web browser, must be written as Java code. Imagine creating all HTML with println. It should feel like a very difficult task.
JSP was created to make server-side Java easier to use. The Java server executes it, but there is actually no mechanism that directly executes Java code written with simple tags.
The Java server reads JSP code and converts it into servlet source code. HTML tags and everything else are converted so they are written with println. The generated servlet source code is compiled, a servlet is created, and that servlet is called. In other words, JSP becomes a servlet.

Although they are the same internally, they feel quite different to users who work with them. JSP can embed processing inside HTML, so it is very convenient when adding something to an HTML page. Servlets, on the other hand, are useful in places where you do not need to output detailed HTML. In other words, it is good to separate responsibilities: use JSP for the frontend, the side displayed to users, and servlets for the backend, the invisible side that runs on the server.
Servlet versions
Before explaining the basic way to create servlets, there is one thing to know: servlet versions.
Servlets are provided as a server-side Java API. Of course, they have continued to improve, and version upgrades sometimes make various features available. The problem is that even if you create a servlet with the latest version, you cannot use it unless the server, or WAS, supports that version.
When using servlets today, it is fair to say the versions are clearly divided into two groups: before 3.0 and after 3.0. Servlet 3.0 introduced major improvements, and the way of creating servlets also changed a lot, although the basic code remains the same. Before 3.0, you had to write configuration files in addition to servlet code. With 3.0, writing only the servlet code is enough and nothing else is needed.
Here, let’s create an example that shows current server, servlet, and JSP information.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Info jsp</title>
<style>
h1 {
font-size: 16pt;
background: #AAFFAA;
padding: 5px;
}
</style>
</head>
<body>
<h1>Sample jsp page</h1>
<p>This page is a sample.</p>
<p>Server info: <%=application.getServerInfo()%></p>
<p>Servlet info: <%=application.getMajorVersion()%>.<%=application.getMinorVersion()%></p>
<p>JSP info: <%=JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion()%></p>
</body>
</html>
In the code above, server and servlet information is obtained from application and displayed, and specification version information is displayed from the JspFactory object.
Servlet versions supported by GAE
GAE actually does not yet support 3.0. It supports the earlier version 2.5. Therefore, compared with the latest version, there are various inconveniences. This introduction uses GAE, but it is basically an introduction to JSP and servlets. Therefore, we will also mention 3.0, but it will not run on GAE. The approach here is to build based on 2.5 while also explaining 3.0, so if GAE later supports it, you can modify and try it yourself.
Servlet version table
| Servlet Spec | JSP Spec | WebSocket Spec | Tomcat Version | Supported Java Version |
|---|---|---|---|---|
| 4.0 | 2.3 | 1.1 | 9.0.x | 8 |
| 3.1 | 2.3 | 1.1 | 8.0.x | 7 |
| 3.0 | 2.2 | 1.1 | 7.0.x | 6 |
| 2.5 | 2.1 | N/A | 6.0.x | 5 |
| 2.4 | 2.0 | N/A | 5.5.x | 1.4 |
| 2.3 | 1.2 | N/A | 4.1.x | 1.3 |
| 2.2 | 1.1 | N/A | 3.3.x | 1.1 |