这将使以下ServletContext
方法(由无数servlet库使用)也可以正常工作
This will allow the following ServletContext
methods (used by countless servlet libraries) to work as well
-
String getRealPath(String path)
-
URL getResource(String path)
-
InputStream getResourceAsStream(String path)
-
Set<String> getResources(String path)
String getRealPath(String path)
URL getResource(String path)
InputStream getResourceAsStream(String path)
Set<String> getResources(String path)
最后,要使ServletContextHandler
中的设置正常,您将在default Servlet名称,该名称恰好实现为DefaultServlet
.
Finally, to make this setup sane in the ServletContextHandler
, you'll add the default
Servlet name, on the "default url-pattern", which happens to be implemented as the DefaultServlet
.
赞:
// Lastly, the default servlet for root content
// It is important that this is added last.
String defName = "default"; // the important "default" name
ServletHolder holderDef = new ServletHolder(defName, DefaultServlet.class);
holderDef.setInitParameter("dirAllowed","true");
ctx.addServlet(holderDef,"/"); // the servlet spec "default url-pattern"
现在,如果您也需要将请求URI /foo/*
中的静态内容从不属于该Web应用程序的目录中提供,您也可以这样做.
这将要求您设置另一个不参与ServletContext
的DefaultServlet
.
Now, if you also have a need to serve static content from request URI /foo/*
out of a directory not belonging to the webapp, you can do that too.
That will require you to setup another DefaultServlet
that does not participate in the ServletContext
.
此设置的一个示例是...
An example of this setup is ...
package jetty;
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;
import org.eclipse.jetty.util.resource.PathResource;
import java.io.File;
import java.nio.file.Path;
public class ManyDefaultServlet
{
public static void main(String[] args) throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
// The filesystem paths we will map
Path homePath = new File(System.getProperty("user.home")).toPath().toRealPath();
Path pwdPath = new File(System.getProperty("user.dir")).toPath().toRealPath();
// 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.setContextPath("/");
context.setBaseResource(new PathResource(pwdPath));
server.setHandler(context);
// Fist, add special pathspec of "/home/" content mapped to the homePath
ServletHolder holderHome = new ServletHolder("static-home", DefaultServlet.class);
holderHome.setInitParameter("resourceBase",homePath.toUri().toASCIIString());
holderHome.setInitParameter("dirAllowed","true");
// Use request pathInfo, don't calculate from contextPath
holderHome.setInitParameter("pathInfoOnly","true");
context.addServlet(holderHome,"/foo/*"); // must end in "/*" for pathInfo to work
// Lastly, the default servlet for root content
// It is important that this is last.
String defName = "default"; // the important "default" name
ServletHolder holderDef = new ServletHolder(defName, DefaultServlet.class);
holderDef.setInitParameter("dirAllowed","true");
context.addServlet(holderDef,"/"); // the servlet spec "default url-pattern"
server.start();
server.join();
}
}
这使用第二个DefaultServlet
,仅使用该DefaultServlet
的唯一资源库,并映射到以/*
结尾的url模式.
This uses a second DefaultServlet
, using a unique resource base for that DefaultServlet
only, and mapped to a url-pattern that ends in /*
.
最后,告诉第二个DefaultServlet
的init参数使用请求URI的pathInfo,而不像通常那样在contextPath上拆分.
Finally, the init-parameter for this second DefaultServlet
is told to use the pathInfo of the Request URI and not split on the contextPath like it normally does.
有关以/*
结尾的整个pathInfo,请求URI,contextPath和url-pattern有关的全部信息,请参见
For more information on what this whole pathInfo, request URI, contextPath, and url-patterns ending in /*
are all about, see the useful answer by @30thh
此独立的DefaultServlet
声明不参与ServletContext
,并且库将无法通过ServletContext
方法查看或访问该DefaultServlet
中的内容.但是,所有传入的HTTP客户端请求都可以通过该url模式轻松地请求内容.
This stand alone DefaultServlet
declaration does not participate in the ServletContext
and libraries will not be able to see or access the content from that DefaultServlet
via the ServletContext
methods. However all incoming HTTP client requests can request the content easily via that url-pattern.
这篇关于码头:默认servlet上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!