我试图按照以下说明操作:
https://www.eclipse.org/jetty/documentation/9.4.x/maven-and-jetty.html
我创建了三个文件:
/Users/dlynch/eclipse-workspace2/swbpp/src/main/java/org/firezeal/SwbppServlet.java
B
/Users/dlynch/eclipse-workspace2/swbpp/src/main/webapp/WEB-INF/web.xml
package org.firezeal;
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 SwbppServlet extends HttpServlet
{
@Override
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());
}
}
/Users/dlynch/eclipse-workspace2/swbpp/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false"
version="3.1">
<servlet>
<servlet-name>swbpp</servlet-name>
<servlet-class>org.firezeal.SwbppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>swbpp</servlet-name>
<url-pattern>/swbpp/*</url-pattern>
</servlet-mapping>
</web-app>
Maven编译项目很好。然后我跑了码头:
<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.firezeal</groupId>
<artifactId>SwbppServlet</artifactId>
<version>1</version>
<packaging>war</packaging>
<name>Jetty HelloWorld WebApp</name>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<jettyVersion>9.4.9.v20180320</jettyVersion>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
</plugin>
</plugins>
</build>
</project>
尝试访问http://127.0.0.1:8080/swbpp时,服务器返回404。
但是,我可以访问/因为我复制了ROOT.war,它显示了一个示例eBay应用程序。
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
线......
2018-04-06 09:46:10.118:INFO:oejsh.ContextHandler:main:已启动oejwWebAppContext @ 4f49f6af {/,[file:/// private / var / folders / t3 / zbrns2cx3396mjbqhnzwfhz00000gn / T / jetty- 0.0.0.0-8080-ROOT.war- -any-10196676678941755136.dir / webapp /,jar:file:///private/var/folders/t3/zbrns2cx3396mjbqhnzwfhz00000gn/T/jetty-0.0.0.0-8080- ROOT.war - -any-10196676678941755136.dir / web应用/ WEB-INF / LIB /示例-异步静止-JAR-9.4.9.v20180320.jar / META-INF /资源],AVAILABLE} {! /ROOT.war}
表示该网址已部署在contextRoot
的{{1}}
因此请尝试/
获取上下文的“基本资源”(也称为主页/根位置)(如果您没有http://127.0.0.1:8080/
,则可能会生成404;如果您的环境已配置,则可能会生成目录列表就这样)
您选择的src/main/webapp/index.html
url-pattern
表示您可以使用以下路径示例来测试该servlet。
/swbpp/*
这是因为Request URI | status | request.getPathInfo()
--------------------------------- | ------ | ---------------------
http://127.0.0.1:8080/swbpp | 404 | (n/a - see below)
http://127.0.0.1:8080/swbpp/ | 200 | "/"
http://127.0.0.1:8080/swbpp/foo | 200 | "/foo"
http://127.0.0.1:8080/swbpp/a/b/c | 200 | "/a/b/c"
不是该url模式的一部分。
如果您有一个名为/swbpp
的文件,那么在请求src/main/webapp/swbpp
时会提供该文件的内容
答案 1 :(得分:0)
根据之前的回复,我了解到正在设置的上下文不正确,而不是更改文件的名称,我了解到您可以从此处生成不同的XML文件:
https://www.eclipse.org/jetty/documentation/9.4.x/configuring-specific-webapp-deployment.html
所以我将SwbppServlet-1.xml文件放入$ {JETTY_BASE} / webapps中,其中包含:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/swbpp</Set>
<Set name="war">/Users/dlynch/Services/Jetty-Base/swbpp/webapps/SwbppServlet-1.war</Set>
<Set name="extractWAR">false</Set>
</Configure>
那时我得到的是目录列表而不是我的代码正在运行。审核后:
Jetty Servlet does not run -- getting directory listing instead
我将WEB_INF / web.xml文件更改为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false"
version="3.1">
<servlet>
<servlet-name>swbpp</servlet-name>
<servlet-class>org.firezeal.SwbppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>swbpp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我认为既然已经设置了上下文,那么url模式应该是/,这对我有用。