简体   繁体   English

如何配置Jetty Handlers?

[英]How to configure Jetty Handlers?

I am encountering a problem with setting up the handlers for my web application, what I want is : Having some requests handled by an HTTPServlet with doGet and doPost methods ( how can I load JSP pages from within these methods ? ) and also be able to load static content ( html, JS, CSS ) 我在为我的Web应用程序设置处理程序时遇到问题,我想要的是:通过HTTPServlet使用doGet和doPost方法处理一些请求(如何从这些方法中加载JSP页面?),并且还能够加载静态内容(html,JS,CSS)

The way I'm setting it right now, I can only have one or the other, I cannot get both working. 我现在设置的方式,只能有一个,而不能同时工作。

I will explain : 我会解释 :

Server server = new Server(5000);

   // This is the resource handler for JS & CSS

    ResourceHandler resourceHandler = new ResourceHandler();

    resourceHandler.setResourceBase(".");

    resourceHandler.setDirectoriesListed(false);

   // This is the context handler for the HTTPServlet

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

    context.setContextPath("/");

    context.addServlet(new ServletHolder(new Main()),"/*");

   // this is the Handler list for both handlers

    HandlerList handlerList = new HandlerList();

    handlerList.setHandlers(new Handler[] { context ,resourceHandler});

/*

     If I add them in this order, all requests will be handled by the "context" and no static resource is loaded

     If I invert the order, the index page page is loaded by the resource handler, which means, If I activate directory listings, it gives me a list of all directories, otherwise it's just a blank page

     I tried working with a WebAppContext to load JSP pages but I still had some problems with which handler should handle which requests

*/

    server.setHandler(handlerList);

    server.start();

    server.join();

Thank you 谢谢

**EDIT : ** **编辑:**

The problem I have is that my HTTP servlet behaves in the following way : Handles all requests leaving nothing for the resource handler, so when a script requests a .js script, it returns an empty html page instead. 我遇到的问题是我的HTTP Servlet行为如下:处理所有请求,不为资源处理程序留下任何东西,因此,当脚本请求.js脚本时,它将返回一个空的html页。 Here is an example : 这是一个例子:

    WebAppContext root = new WebAppContext();

    root.setParentLoaderPriority(true);
    root.setContextPath("/");
    root.setResourceBase(".");
    root.setWelcomeFiles(new String[] {"test.jsp"});
    root.addServlet(new ServletHolder(new Main()),"/*");

    HandlerList handlerList = new HandlerList();
    handlerList.setHandlers(new Handler[] { root });

In this example, when using the root handler without the Main servlet, It loads all static content and jsp pages, but when adding the main servlet, it no longer loads any static content, and responds to all request for static content with an empty html page. 在此示例中,当使用不带Main servlet的root处理程序时,它将加载所有静态内容和jsp页面,但是在添加main servlet时,它将不再加载任何静态内容,并使用空的html响应对静态内容的所有请求页。

When you work with servlets, there is always a termination at the end of the servlet chain. 当您使用servlet时,servlet链的末尾总是会有一个终结点。

It would be either: 可能是:

If all you want the ResourceHandler for is serving static content, use the DefaultServlet for your own purposes (its a better choice and supports more functionality as well. such as range requests, caching, auto-gzip, memory-mapped file serving, etc) 如果您希望ResourceHandler提供的全部内容都是静态内容,请使用DefaultServlet满足自己的目的(它是更好的选择,还支持更多功能,例如范围请求,缓存,自动gzip,内存映射文件提供等)。

Example: 例:

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;

public class ManyDefaultServlet
{
    public static void main(String[] args)
    {
        System.setProperty("org.eclipse.jetty.servlet.LEVEL","DEBUG");

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

        // 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("/");
        server.setHandler(context);

        // The filesystem paths we will map
        String homePath = System.getProperty("user.home");
        String pwdPath = System.getProperty("user.dir");

        // Fist, 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
        // It is important that this is last.
        ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
        holderPwd.setInitParameter("resourceBase",pwdPath);
        holderPwd.setInitParameter("dirAllowed","true");
        context.addServlet(holderPwd,"/");

        try
        {
            server.start();
            server.dump(System.err);
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM