如何从Jar(而非战争)中获取嵌入的Jetty服务html文件 [英] How to get embedded Jetty serving html files from a Jar, not a War

查看:123
本文介绍了如何从Jar(而非战争)中获取嵌入的Jetty服务html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何获得嵌入式码头服务器来服务同一罐子中包含的少数html文件的尝试,但没有成功.当然可以吗?

I have been searching without success in how to get an embedded jetty server to serve a handful of html files that are contained within the same jar. Surely this is possible?

如果不需要的话,我真的不想经历建造和打仗的麻烦.理想情况下,尽管我阅读的所有解决方案似乎都指向执行此操作并使用WebAppContext,但我也不必创建WEB-INFO目录和web.xml文件.

I really don't want to go through the hassle of building and working with a war if I don't have to. Ideally I wouldn't have to create a WEB-INFO dir and a web.xml files either, though all solutions I've read seem to point to doing this and using a WebAppContext.

我已阅读以下链接,但尚未找到从jar运行时设置ResourceBase或BaseResource属性的方法.

I've read the following links, but haven't found a way to set the ResourceBase or BaseResource property when running from a jar.

使用没有WAR文件的码头启动Java应用程序

什么是正确的用于指定JAR"resources/webapp"的ResourceBase的URL.嵌入式Jetty的文件夹?

嵌入式码头在其Jar文件中查找文件

在DEV期间通过IDE运行非常简单,代码正常工作,看起来像这样.

Running through an IDE during DEV it was simple, the code worked and looked something like this..

Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(httpPort);
server.addConnector(connector);

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setWelcomeFiles(new String[]{"welcome.html"});
server.setHandler(context);

ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
holderPwd.setInitParameter("resourceBase","./Relative/Path/To/Html/Files");
holderPwd.setInitParameter("dirAllowed","true");
context.addServlet(holderPwd,"/");

server.start();
server.join();

那么,我必须使用WebAppContext而不是ServletContextHandler吗?如果是,那么我也必须添加一个webapp/WEB-INFO/web.xml目录结构吗?如果我这样做了,那么我是否必须打包成战争?

So, do I have to use WebAppContext instead of ServletContextHandler? If yes, then do I have to add a webapp/WEB-INFO/web.xml directory structure too? And if I do that, then do I have to package as a war?

推荐答案

您需要将上下文的资源库设置为可以从中访问静态内容的URL/URI.

You need to set the Resource Base for the Context to the URL/URI to where your static content can be accessed from.

注意:您在ServletContext级别(而不是DefaultServlet级别)进行设置,这样您上下文中的所有servlet都可以访问相同的信息以及与实际文件路径和资源相关的ServletContext中的各种方法.很理智.

Note: you set this at the ServletContext level, not the DefaultServlet level, that way all servlets in your context have access to the same information and the various methods in ServletContext related to real file paths and resources are sane.

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

    // Figure out what path to serve content from
    ClassLoader cl = MyEmbeddedJettyMain.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("static-root/hello.html");
    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(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setBaseResource(Resource.newResource(webRootUri));
    context.setWelcomeFiles(new String[]{"welcome.html"});

    ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
    holderPwd.setInitParameter("dirAllowed","true");
    context.addServlet(holderPwd,"/");

    server.setHandler(context);

    server.start();
    server.join();
}

这篇关于如何从Jar(而非战争)中获取嵌入的Jetty服务html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆