@WebServlet标注不与Tomcat的8个工作@WebServlet标注不与Tomcat的8个

2019-05-12 02:39发布

我想使用@WebServlet标注在它运行在Tomcat 8在Java EE Web应用程序。

我已阅读,我需要声明的Servlet 3.1版在我web.xml和我的servlet需要扩展HttpServlet 。 我做过的一切,但仍是@WebServlet不起作用。 我得到一个HTTP 404。

我也试过我的配置与metadata-complete="false"在我web.xml ,但仍然没有成功。

这是我的web.xml和Servlet。

完整的示例代码可以发现在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();
    }
  }

}

Answer 1:

我得到它的工作。 我不得不延长我开始了我的Tomcat 8.0.12服务器的方式。

不过,也有必须要做的三个主要方面:

  1. web-app versionweb.xml必须是至少3.0( 我用3.1)
  2. metadata-complete web.xml中可能不是真的( 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();
}


Answer 2:

尝试添加此:

((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);


Answer 3:

如何@ServletComponentScan?

来自实施例http://www.baeldung.com/spring-servletcomponentscan

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


文章来源: @WebServlet annotation doesn't work with Tomcat 8