我正在阅读Jetty here的Hello World教程
根据本教程,我应该能够在:8080 / hello-world / hello(hello-world来自pom.xml中的工件ID,而hello来自web.xml)中访问它。尝试此操作时,出现HTTP错误404。
当我尝试访问位于http://172.17.0.2:8080/(172.17.0.2是容器的ip地址)的Web服务器时,显示以下信息:
Error 404 - Not Found.
No context on this server matched or handled this request.
Contexts known to this server are:
- /hello-world-0.1-SNAPSHOT ---> o.e.j.w.WebAppContext@3224f60b{hello-world-0.1-SNAPSHOT,/hello-world-0.1-SNAPSHOT,file:///tmp/jetty-0.0.0.0-8080-hello-world-0.1-SNAPSHOT.war-_hello-world-0.1-SNAPSHOT-any-4591378293374546697.dir/webapp/,AVAILABLE}{/hello-world-0.1-SNAPSHOT.war}
我的Dockerfile:
FROM openjdk:10 as step-one
COPY ./ /var/www/java/
WORKDIR /var/www/java
RUN apt-get update -y && apt-get install -y maven
RUN mvn clean package -X
FROM jetty as step-two
COPY --from=step-one /var/www/java/target/hello-world-0.1-SNAPSHOT.war /var/lib/jetty/webapps/
ENTRYPOINT ["java"]
CMD ["-jar", "/usr/local/jetty/start.jar", "--create-startd=jmx, stats"]
EXPOSE 8080
我的web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>org.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hello-world</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Jetty HelloWorld</name>
<properties>
<jettyVersion>7.2.0.v20101020</jettyVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<!-- This plugin is needed for the servlet example -->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution><goals><goal>java</goal></goals></execution>
</executions>
<configuration>
<mainClass>org.example.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
HelloServlet.java:
package org.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello Servlet</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}