Java 从嵌入式 Jetty 中的备用路径提供静态文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20207477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serving static files from alternate path in embedded Jetty
提问by user3034643
I'm trying to create an embedded jetty server with both a custom servlet that serves some dynamic data, and default servlet that will serve some images. I have the custom servlet working, but I can't figure out how to add a default servlet to serve the image files.
我正在尝试使用提供一些动态数据的自定义 servlet 和将提供一些图像的默认 servlet 创建一个嵌入式码头服务器。我有自定义 servlet 工作,但我不知道如何添加默认 servlet 来提供图像文件。
This is what I have...
这就是我所拥有的...
private void setUpServer(ServerOptions options){
s = new Server(options.getPort());
this.options = options;
context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
s.setHandler(context);
context.addServlet(new ServletHolder(new DataServlet()), "/data/*");
context.addServlet(new ServletHolder(new DefaultServlet()), "/pictures/*");
}
I can't figure out how to configure the DefaultServlet to work as a file server and still have the custom DataServelet still work.
我不知道如何将 DefaultServlet 配置为作为文件服务器工作并且仍然让自定义 DataServelet 仍然工作。
Does anyone have any ideas?
有没有人有任何想法?
采纳答案by Joakim Erdfelt
What you need:
你需要什么:
- A DefaultServlet at "/" (recommended, it is a requirement of the servlet spec)
- this should be at named dispatcher of "default" (another requirement of servlet spec)
- An alternate DefaultServlet with your custom static content, configured via init-params
- Using a different named dispatcher than "default" (to avoid a name collision between other servlet spec features)
- You can use the
ServletHolder.setInitParameter(name,value)
to accomplish this - Be sure you set the
pathInfoOnly
parameter totrue
(to get around special cases for "default" named dispatchers)
- A servlet of your own, serving dynamic content.
- “/”处的 DefaultServlet(推荐,这是 servlet 规范的要求)
- 这应该在“默认”的命名调度程序(servlet规范的另一个要求)
- 带有自定义静态内容的备用 DefaultServlet,通过 init-params 配置
- 使用与“默认”不同的命名调度程序(以避免其他 servlet 规范功能之间的名称冲突)
- 您可以使用
ServletHolder.setInitParameter(name,value)
来完成此操作 - 确保将
pathInfoOnly
参数设置为true
(以避开“默认”命名调度程序的特殊情况)
- 您自己的 servlet,提供动态内容。
AltDefaultServlet.java
AltDefaultServlet.java
package jetty.demo;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class AltDefaultServlet
{
public static void main(String[] args)
{
System.setProperty("org.eclipse.jetty.LEVEL","INFO");
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
// The filesystem paths we will map
String homePath = System.getProperty("user.home");
String pwdPath = System.getProperty("user.dir");
// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(Resource.newResource(pwdPath));
context.setContextPath("/");
server.setHandler(context);
// add a simple Servlet at "/dynamic/*"
ServletHolder holderDynamic = new ServletHolder("dynamic", DynamicServlet.class);
context.addServlet(holderDynamic, "/dynamic/*");
// add special pathspec of "/home/" content mapped to the homePath
ServletHolder holderHome = new ServletHolder("static-home", DefaultServlet.class);
holderHome.setInitParameter("resourceBase",homePath);
holderHome.setInitParameter("dirAllowed","true");
holderHome.setInitParameter("pathInfoOnly","true");
context.addServlet(holderHome,"/home/*");
// Lastly, the default servlet for root content (always needed, to satisfy servlet spec)
// It is important that this is last.
ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
holderPwd.setInitParameter("dirAllowed","true");
context.addServlet(holderPwd,"/");
try
{
server.start();
server.dump(System.err);
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
}
}