码头:默认servlet上下文路径 [英] Jetty: default servlet context path

查看:75
本文介绍了码头:默认servlet上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置Servlet(由于某些原因,只有Servlet而不是处理程序)才能与war以外的文件一起使用.在 https://stackoverflow.com/a/28735121/5057736 我找到了以下解决方案:

I need to set Servlet (only servlet not handler because of some reasons) to work with files outside war. Here https://stackoverflow.com/a/28735121/5057736 I found the following solution:

Server server = new Server(8080);

ServletContextHandler ctx = new ServletContextHandler();
ctx.setContextPath("/");

DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", "./src/webapp/");

ctx.addServlet(holderPwd, "/*");//LINE N
ctx.addServlet(InfoServiceSocketServlet.class, "/info");

server.setHandler(ctx);

此解决方案有效,这就是我所需要的.但是,一旦将LINE N更改为ctx.addServlet(holderPwd, "/foo/*");,它就会停止工作.我尝试了"/foo/","/foo",但是结果是相同的-我得到了not found.为什么?如何在特定背景下使用它?由于相同的原因,我使用码头9.2.15.

This solutions works and this is what I need. However, it stops working as soon as I change LINE N to ctx.addServlet(holderPwd, "/foo/*");. I tried "/foo/","/foo" but result is the same - I get not found. Why? How can I make it work with this certain context? I use jetty 9.2.15 because of the same reasons.

推荐答案

DefaultServlet旨在查看contextPath之后的请求URI.

The DefaultServlet is designed to look at the request URI after the contextPath.

在示例代码中,当您将servlet的url模式从/更改为/foo/*时,正在磁盘上查找的结果文件现在包括/foo/部分.

In your example code when you changed the url-pattern of your servlet from / to /foo/* the resulting file being looked for on disk is now includes the /foo/ portion.

换句话说,请求URI /css/main.css会导致文件(在磁盘上)期望以./src/webapp/foo/css/main.css

In other words, a request URI of /css/main.css results in the file (on disk) it expects to find as ./src/webapp/foo/css/main.css

您的示例有一些缺陷. ServletContextHandler拥有空的资源库是不明智的,因为ServletContext本身需要访问该配置值.

Your example has a few flaws. It's not a wise have an empty resource base for your ServletContextHandler, as the ServletContext itself needs access to that configuration value.

您可以通过删除...来解决此问题

You would fix that by removing ...

holderPwd.setInitParameter("resourceBase", "./src/webapp/");

并使用

这将使以下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应用程序的目录中提供,您也可以这样做. 这将要求您设置另一个不参与ServletContextDefaultServlet.

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屋!

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