1
2
3
4
5
6
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
Your Maven project will have:
1
2
3
4
5
6
7
8
jsp-shop/
├─ src/main/java/ (Java source files)
├─ src/main/resources/ (Config files)
├─ src/main/webapp/ (JSP, HTML, CSS, JS)
│ ├─ META-INF/
│ ├─ WEB-INF/
│ │ └─ web.xml
└─ pom.xml (Maven configuration)
Add dependencies for JSP and Servlet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependencies>
<!-- Servlet API -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
Note: Scope provided means the server (Tomcat) will provide these libraries at runtime.
1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Welcome to JSP Shop</title>
</head>
<body>
<h1>Hello, JSP World!</h1>
</body>
</html>
If you want to set a default welcome page:
1
2
3
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Step 7: Build & Run
The browser should open:
1
http://localhost:8080/jsp-shop
You’ll see Hello, JSP World!