【问题标题】:Build WAR and self-executable JAR from Java web project从 Java Web 项目构建 WAR 和自执行 JAR
【发布时间】:2020-03-09 16:01:51
【问题描述】:

我有一个 Java servlets 项目,我想构建一个 WAR 以部署到服务器和一个可执行 JAR,其中包含可以独立运行的嵌入式 Jetty 服务器。 该项目如下所示:

HelloWorldServlets
  common
    src/main
      java
        HelloServlet.java
      webapp
        index.jsp
    build.gradle
  jetty
    src/main
      java
        JettyServer.java
    build.gradle
  build.gradle

jetty 导入 common 作为编译依赖。构建任务是:

jar {
  manifest { attributes "Main-Class": "JettyServer" }
  from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) })
}

common中的jar任务如下:

jar {
  sourceSets.main.resources.srcDirs += ["src/main/webapp"]
}

Web 资源包含在common.jar 中,common.jarjetty.jar 中被展平,因此jetty.jar 的资源位于其根目录中,但是将服务器设置为:

Server server = new Server(8080);
WebAppContext context = new WebAppContext();
server.setHandler(context);

context.setResourceBase("/");
context.setContextPath("/");
context.addServlet(Hello.class, "");

总是导致 HTTP ERROR 404 访问 /index.jsp 时出现问题。

我不应该将common.jar 扁平化为jetty.jar 还是我缺少关于ResourceBase 的一些东西?谢谢!

【问题讨论】:

    标签: java servlets jetty embedded-jetty


    【解决方案1】:

    您的代码示例中的一些注意事项。

    • 由于您希望使用 JSP,因此您必须满足 JSP 的最低要求。
    • setResourceBase(String) 必须是绝对路径和/或 URI 引用(无相对路径)(JSP 要求)
    • 必须正确定义 DefaultServlet。 (JSP 要求)
    • 添加 Servlet(或过滤器)时,您必须使用有效的 url 模式。 (Servlet 要求)
    • 在您的处理程序树中最后添加DefaultHandler,因为它有助于您解决上下文和/或处理程序错误。

    出现问题的最常见原因是使用 WebAppContext 会导致强制执行标准 Servlet 类加载器隔离,这很可能会导致您出现问题。

    要么打破类加载器......

    Server server = new Server(8080);
    
    // Figure out what path to serve content from
    ClassLoader cl = DemoProject.class.getClassLoader();
    // We look for a file, as ClassLoader.getResource() is not
    // designed to look for directories (we resolve the directory later)
    URL f = cl.getResource("jsp-root/index.jsp");
    if (f == null)
    {
        throw new RuntimeException("Unable to find resource directory");
    }
    
    // Resolve file to directory
    URI webRootUri = f.toURI().resolve("./").normalize();
    System.err.println("WebRoot is " + webRootUri);
    
    WebAppContext context = new WebAppContext();
    context.setBaseResource(Resource.newResource(webRootUri));
    context.setContextPath("/");
    context.addServlet(Hello.class, "/hello");
    context.setParentLoaderPriority(true); // use server classloader first
    
    HandlerList handlers = new HandlerList();
    handlers.addHandler(context);
    handlers.addHandler(new DefaultHandler()); // for errors when request doesn't match above handlers
    server.setHandler(handlers);
    

    ...或者只是不使用 WebAppContext ...

    Server server = new Server(8080);
    
    // Figure out what path to serve content from
    ClassLoader cl = DemoProject.class.getClassLoader();
    // We look for a file, as ClassLoader.getResource() is not
    // designed to look for directories (we resolve the directory later)
    URL f = cl.getResource("jsp-root/index.jsp");
    if (f == null)
    {
        throw new RuntimeException("Unable to find resource directory");
    }
    
    // Resolve file to directory
    URI webRootUri = f.toURI().resolve("./").normalize();
    System.err.println("WebRoot is " + webRootUri);
    
    ServletContextHandler context = new ServletContextHandler();
    context.setBaseResource(Resource.newResource(webRootUri));
    context.setContextPath("/");
    context.addServlet(Hello.class, "/hello");
    
    // Must be added last, must be named "default" (JSP and Servlet requirement)
    ServletHolder holderDef = new ServletHolder("default", DefaultServlet.class);
    holderDef.setInitParameter("dirAllowed","false");
    context.addServlet(holderDef,"/"); // Using "default" url-pattern (JSP and Servlet requirement)
    
    HandlerList handlers = new HandlerList();
    handlers.addHandler(context);
    handlers.addHandler(new DefaultHandler()); // for errors when request doesn't match above handlers
    server.setHandler(handlers);
    

    但请注意,要在 ServletContextHandler 上启用 JSP,您还有更多工作要做。

    详情见:https://github.com/jetty-project/embedded-jetty-jsp

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 2017-10-28
      • 1970-01-01
      • 2015-12-12
      • 2012-11-02
      • 2014-01-15
      • 2010-11-24
      • 1970-01-01
      • 2014-04-15
      相关资源
      最近更新 更多