【问题标题】:annotations not working with jetty stand alone server with servlet注释不适用于带有 servlet 的 jetty 独立服务器
【发布时间】:2015-12-13 03:04:48
【问题描述】:

我正在尝试使用 servlet 和注释创建一个码头独立 Web 服务器的简单示例。如果我使用 xml 将 URL 映射到它可以工作的方法,但是当我尝试使用 @WebServlet() 时,码头服务器没有成功地将请求映射到方法。有谁知道我可能会错过什么?我在网上搜索了一些例子,但没有发现任何遗漏。谢谢。

我的 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>

【问题讨论】:

  • 我忘了提到我正在 Eclipse 中开发它并导出为 WAR 文件,然后将其放入码头独立 /webapps 文件夹中。

标签: annotations jetty


【解决方案1】:

使用 Jetty Distribution 9.3.x,将以下内容添加到您的 ${jetty.base}/start.ini

--module=annotations

请勿更改您的 ${jetty.home}。不要在您的${jetty.home} 目录中编辑、删除、重命名、移动、触摸、符号链接或任何其他内容。不理会那个目录。正确使用${jetty.base},您将拥有更容易维护的实例配置,以及脑死的简单 Jetty 升级。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-10-29
  • 2012-12-06
  • 2023-04-07
  • 1970-01-01
  • 2014-10-25
  • 2022-01-08
  • 2017-06-03
  • 1970-01-01
相关资源
最近更新 更多