【问题标题】:@WebServlet annotation doesn't work with Tomcat 8@WebServlet 注释不适用于 Tomcat 8
【发布时间】:2014-09-28 21:35:51
【问题描述】:

我想在运行在 Tomcat 8 上的 Java EE webapp 中使用@WebServlet 注解。

我已经读到我需要在 web.xml 中声明 Servlet 版本 3.1,并且我的 Servlet 需要扩展 HttpServlet。我做了所有这些,但@WebServlet 仍然不起作用。我收到了 HTTP 404。

我还在web.xml 中尝试使用metadata-complete="false" 进行配置,但仍然没有成功。

这是我的 web.xml 和 Servlet。

完整的示例代码可以在on GitHub找到。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
  version="3.1" 
  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">

  <context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
  </context-param>

  <!-- http://stackoverflow.com/a/7924117/451634 -->
  <!-- Put "-1" to disable this feature -->
  <context-param>
    <param-name>facelets.REFRESH_PERIOD</param-name>
    <param-value>1</param-value>
  </context-param>

  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF -->
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>   

  <!-- CDI -->
  <listener>
    <listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
  </listener>

</web-app>

TestServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "TestServlet", urlPatterns = {"*.serve"})
public class TestServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
    try (ServletOutputStream out = resp.getOutputStream()) {
      out.write("Hello World".getBytes());
      out.flush();
    }
  }

}

【问题讨论】:

    标签: servlets embedded-tomcat-8


    【解决方案1】:

    我让它工作了。我不得不扩展启动 Tomcat 8.0.12 服务器的方式。

    然而,必须做三件主要的事情:

    1. web.xml 中的web-app version 必须至少为 3.0(我使用的是 3.1
    2. web.xml 中的metadata-complete 可能不是真的 (default is "false")
    3. classes 目录必须在启动前添加到嵌入式 Tomcat 中

    这是 web.xml 文件的示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 
      version="3.1" 
      metadata-complete="false"  
      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">
    
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    
    </web-app>
    

    这就是我的 Tomcat 主类的样子(现在支持 @WebServlet 注释):

    public static void main(String[] args) throws Exception {
      String contextPath = "/";
      String webappDirLocation = "src/main/webapp/";
      String baseDirectory = new File(webappDirLocation).getAbsolutePath();
    
      Tomcat tomcat = new Tomcat();
      tomcat.setPort(8080);
      StandardContext context = (StandardContext) tomcat.addWebapp(contextPath, baseDirectory);
    
      // Additions to make @WebServlet work
      String buildPath = "target/classes";
      String webAppMount = "/WEB-INF/classes";
    
      File additionalWebInfClasses = new File(buildPath);
      WebResourceRoot resources = new StandardRoot(context);
      resources.addPreResources(new DirResourceSet(resources, webAppMount, additionalWebInfClasses.getAbsolutePath(), contextPath));
      context.setResources(resources);
      // End of additions
    
      tomcat.start();
      tomcat.getServer().await();
    }
    

    【讨论】:

    【解决方案2】:

    @ServletComponentScan 怎么样?

    来自http://www.baeldung.com/spring-servletcomponentscan的示例

    @ServletComponentScan
    @SpringBootApplication
    public class SpringBootAnnotatedApp {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootAnnotatedApp.class, args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 2014-07-28
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 2023-04-07
      • 2017-12-21
      • 1970-01-01
      • 2017-10-11
      相关资源
      最近更新 更多