Servlets @WebServlet注释不';我不能使用Tomcat 8

Servlets @WebServlet注释不';我不能使用Tomcat 8,servlets,embedded-tomcat-8,Servlets,Embedded Tomcat 8,我想在运行在Tomcat8上的JavaEEWebApp中使用@WebServlet注释 我已经读到,我需要在web.xml中声明Servlet版本3.1,并且我的Servlet需要扩展HttpServlet。我做了所有这些,但是@WebServlet仍然不起作用。我得到一个HTTP 404 我还尝试在我的web.xml中使用metadata complete=“false”进行配置,但仍然没有成功 这是我的web.xml和Servlet 可以找到完整的示例代码 web.xml <?xml

我想在运行在Tomcat8上的JavaEEWebApp中使用
@WebServlet
注释

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

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

这是我的web.xml和Servlet

可以找到完整的示例代码

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>

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

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

  • web.xml中的web应用程序版本必须至少为3.0(我使用了3.1)
  • web.xml中的元数据完成
    可能不正确(
    默认值为“false”
  • 目录必须在启动之前添加到嵌入式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>
    

    @ServletComponentScan怎么样

    来自


    这有时有效,但有时会导致错误。另请参见
    <?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>
    
    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();
    }
    
    @ServletComponentScan
    @SpringBootApplication
    public class SpringBootAnnotatedApp {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootAnnotatedApp.class, args);
        }
    }