--Run Java Servlet and JSP in local environment --Use Jetty for Servlet container --Launch Jetty with Maven plugin
--pom.xml: Build config file for Maven --MyServlet.java: Java Servlet source code --index.jsp: JSP source code --web.xml: Web application configuration file
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ └── MyServlet.java
└── webapp
├── WEB-INF
│ └── web.xml
└── index.jsp
--Add javax.servlet-api to dependency to use Java Servlet API 3.1. --Add jetty-maven-plugin so that you can easily start Jetty from Maven and check the operation.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-servlet</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>my-servlet</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jetty.version>9.4.19.v20190610</jetty.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
</project>
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html><body>Hello, Servlet World!</body></html>");
out.flush();
out.close();
}
}
<html><body>Hello JSP World!</body></html>
--Mapping the Servlet of MyServlet.java so that it can be accessed with / my-servlet / *.
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>My Servlet Web Application</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/my-servlet/*</url-pattern>
</servlet-mapping>
</web-app>
You can start Jetty with mvn jetty: run.
$ mvn jetty:run
When you access http: // localhost: 8080 / my-servlet /, the content output by MyServlet.java is returned.
$ curl http://localhost:8080/my-servlet/
<html><body>Hello, Servlet World!</body></html>
When you access http: // localhost: 8080 /, the content written in index.jsp is returned.
$ curl http://localhost:8080/
<html><body>Hello JSP World!</body></html>
Recommended Posts