我正在尝试使用servlet和注释创建一个简单的jetty独立Web服务器示例。如果我使用xml将URL映射到它工作的方法,但是当我尝试使用@WebServlet()时,jetty服务器无法将请求成功映射到方法。有谁知道我可能会缺少什么?我在网上搜索了一些例子,但没有看到任何遗漏。谢谢。
我的servlet是这样的:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorldServlet3
*/
@WebServlet("/hello")
public class HelloWorldServlet3 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloWorldServlet3() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
web.xml文件是:
<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_3_0.xsd"
version="3.0">
和启动服务器的linux命令行是:
java -jar $ JETTY_HOME / start.jar
作为旁注:
如果我使用以下xml它可以工作 - 但我不需要使用注释:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test_jetty</display-name>
<servlet>
<servlet-name>helloWorldServlet3</servlet-name>
<servlet-class>com.xyz.www.HelloWorldServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorldServlet3</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
使用Jetty Distribution 9.3.x,将以下内容添加到${jetty.base}/start.ini
--module=annotations
不要在您的
${jetty.home}
中进行更改。请勿编辑,删除,重命名,移动,触摸,符号链接或${jetty.home}
目录中的任何其他内容。保留该目录。正确使用${jetty.base}
,您将拥有更容易维护的实例配置,以及易死的Jetty升级。