本文介绍了嵌入式Jetty处理URL来提供内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在将嵌入式Jetty与Guice一起使用,并且想知道处理为单页应用程序提供服务的最佳方法.

I am using embedded Jetty with Guice and am wondering the best way to handle serving my single page application.

我希望码头能像这样处理请求(按优先顺序):

I wish for Jetty to handle requests like so (in priority order):

  • /socket必须由websocket servlet处理

  • /socket must be handled by the websocket servlet

/fs/read/*,与该网址匹配的所有内容都需要由自定义servlet处理

/fs/read/*, anything that matches this url I need to be handled by a custom servlet

现在我想知道处理此问题的最佳方法吗?使用REST框架似乎很费力,因为我实际上没有任何休息服务.

Now I am wondering the best way to handle this? It seems heavy handed to use a REST framework as I don't really have any rest services.

当前是我设置Jetty的方式:

Here is currently how I am setting up Jetty:

ServletHolder servletHolder = new ServletHolder(DefaultServlet.class);

ServletContextHandler servletContextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
servletContextHandler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
servletContextHandler.addServlet(servletHolder, "/");

ResourceHandler resHandler = new ResourceHandler();
resHandler.setBaseResource(Resource.newClassPathResource("/web"));
server.setHandler(resHandler);

这是我的Guice ServletModule:

Here is my Guice ServletModule:

serve("/socket/*").with(WebSocketManagerServlet.class);
serve("/fs/read/*").with(MyCustomServlet.class);

但是,我不确定如何执行最后一条规则,在该规则中它将不匹配index.html的任何内容重定向,并且仍然让jetty发送正确的标头,这些标头是css/js/html的正确mime类型,等等.Jetty也可能会这样做一些内存映射和一些花哨的东西可以快速地为我服务,这些都是我想要维护的.

However, I am not sure how to do the last rule where it redirects anything not matched to index.html and still have jetty send out the right headers which the correct mime types for css/js/html, etc. Jetty also probably does some memory mapping and fancy things to serve these quickly which I'd like to maintain.

推荐答案

根据您的要求,我认为您想要这个...

Based on what you have asked, I think you want this ...

URL urlToWebDir = findUrlTo("/web");
ServletContextHandler servletContextHandler =
    new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.setWelcomeFiles(new String[] { "index.html" });
servletContextHandler.setBaseResource(Resource.newResource(urlToWebDir));
servletContextHandler.addServlet(DefaultServlet.class, "/");

ErrorPageErrorHandler errorMapper = new ErrorPageErrorHandler();
errorMapper.addErrorPage(404,"/"); // map all 404's to root (aka /index.html)
servletContextHandler.setErrorHandler(errorMapper);

这是针对您的"/web"路径要求的更简单设置.

This is a simpler setup for your "/web" path requirement.

BaseResource是URL(可以是jar:file://file:// URL引用),DefaultServlet使用该URL来提供您的静态内容.

The BaseResource is the URL (it can be a jar:file:// or file:// URL reference) which is used by the DefaultServlet to serve your static content.

因此,如果在/image.png上发出请求,则将提供{baseResource}/image.png(并且从{baseResource}/icons/avatar.gif

So that if a request comes in on /image.png then {baseResource}/image.png is served (and a request on /icons/avatar.gif is served from {baseResource}/icons/avatar.gif

WelcomeFiles设置index.html解析,以便对/的请求导致提供文件{baseResource}/index.html. (如果请求为/alternate/path/deep/in/your/tree/,则也可以使用/alternate/path/deep/in/your/tree/index.html(如果存在).

The WelcomeFiles sets up the index.html resolving, so that a request on / results in serving file {baseResource}/index.html. (also works if request is /alternate/path/deep/in/your/tree/ then /alternate/path/deep/in/your/tree/index.html is served (if it exists).

奖金说明:DefaultServlet支持预压缩的静态内容投放.这意味着,如果您对内容进行gzip压缩并保留其副本,那么如果用户的浏览器说它支持压缩的响应,则压缩版本将提供给用户. (例如:使用不支持压缩响应的旧浏览器请求/js/hugelib.js,则投放{baseResource}/js/hugelib.js.但是,如果浏览器支持压缩响应,则投放{baseResource}/js/hugelib.js.gz)

Bonus note: DefaultServlet supports pre-compressed static content serving. Meaning if you gzip your content and keep a copy of it along-side, then the compressed version is served to your users if their browser says it supports compressed responses. (example: request for /js/hugelib.js with old browser that doesn't support compressed responses, then {baseResource}/js/hugelib.js is served. But if browser supports compressed responses, then {baseResource}/js/hugelib.js.gz is served instead)

这篇关于嵌入式Jetty处理URL来提供内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 22:36