【问题标题】:Servlet 3.0 without a WAR file?没有 WAR 文件的 Servlet 3.0?
【发布时间】:2013-03-31 09:55:16
【问题描述】:

是否可以通过以下方式启动 webapp:

1) 没有战争文件:

http://stephenh.github.io/2009/01/10/war-less-dev-with-jetty.html http://www.jamesward.com/2011/08/23/war-less-java-web-apps

2) 没有 web.xml(即 Servlet-3.0)

3) 从嵌入式 Web 容器(例如 Tomcat 或 Jetty...)

【问题讨论】:

  • 你可以在没有容器的情况下“部署”一个 JAX-WS 服务!
  • 只是寻找一个简单的 webapp 设置来处理基本的浏览器 http 请求...(不是 SOAP)

标签: java tomcat web-applications jetty servlet-3.0


【解决方案1】:

示例项目:https://github.com/jetty-project/embedded-servlet-3.0

您仍然需要WEB-INF/web.xml,但需要it can be empty。这样才能知道 servlet 支持级别和元数据完整标志。

示例:空的 Servlet 3.0 web.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"
    metadata-complete="false"
    version="3.0">
</web-app>

然后您可以关注EmbedMe.java 获取有关如何设置的示例。

public class EmbedMe {
    public static void main(String[] args) throws Exception {
        int port = 8080;
        Server server = new Server(port);

        String wardir = "target/sample-webapp-1-SNAPSHOT";

        WebAppContext context = new WebAppContext();
        context.setResourceBase(wardir);
        context.setDescriptor(wardir + "WEB-INF/web.xml");
        context.setConfigurations(new Configuration[] {
                new AnnotationConfiguration(), new WebXmlConfiguration(),
                new WebInfConfiguration(), new TagLibConfiguration(),
                new PlusConfiguration(), new MetaInfConfiguration(),
                new FragmentConfiguration(), new EnvConfiguration() });

        context.setContextPath("/");
        context.setParentLoaderPriority(true);
        server.setHandler(context);
        server.start();
        server.join();
    }
}

【讨论】:

  • 不起作用,我在尝试访问 localhost:8080 时遇到 404 错误(我正在从 Eclipse 运行它,就像“Java 应用程序”一样)。我也在使用最新的github.com/jetty-project/embedded-servlet-3.0 :(
  • 在该项目上尝试命令行... $ mvn clean install exec:exec
  • 我是否仍需要应用此解决方法:bugs.eclipse.org/bugs/show_bug.cgi?id=404176 才能使用您在 WEB-INF/ 之外显示的“AnnotationConfig()”?
  • 此解决方案打包为 WAR
  • @fionbio 它是一个爆炸的 webapp 目录。 WAR 是一个网络存档。
【解决方案2】:

我是怎么做到的(嵌入 SpringMVC + Jetty,没有 web.xml 没有战争文件):

使用 Spring 的 @WebAppConfigurationMockServletContext 引导您的 WebApplicationContext

然后只需通过 Jetty 的 ServletContextHandler/ServletHolder 机制注册您的new DispatcherServlet(WebApplicationContext)。简单!

【讨论】:

    【解决方案3】:

    可以使用 Jetty 实现不需要 WAR 或任何 XML 的嵌入式服务器。您只需指定带注释的类的位置,添加额外的类路径。

    这个方法应该从你可以命名为Server.java的main调用:

    private static void startServer() throws Exception {
        final org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(7070);
        final WebAppContext context = new WebAppContext("/", "/");
        context.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebInfConfiguration() });
        context.setExtraClasspath("build/classes/main/com/example/servlet");
        server.setHandler(context);
        server.start();
        server.join();
    }
    

    我的src 结构是:

    -main
      -java
        -com.example
          -json
          -servlet
          -filter
          -util
          Server.java
    

    我希望看到与 Tomcat 类似的解决方案。

    【讨论】:

      【解决方案4】:

      【讨论】:

      • 不是我想要的;我实际上是在尝试以编程方式将 webapp(而不是简单的手动 HttpServer)加载到嵌入式 Web 容器中,而无需部署“war 文件”甚至拥有 web.xml。具体来说,一个 Spring WebApplicationContext...
      猜你喜欢
      • 2011-04-07
      • 2016-09-20
      • 2017-12-03
      • 1970-01-01
      • 2019-10-01
      • 2017-01-19
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多