如何配置码头处理程序?
How to configure Jetty Handlers?
我在为我的 Web 应用程序设置处理程序时遇到问题,我想要的是:让 HTTPServlet 使用 doGet 和 doPost 方法处理一些请求(如何从内部加载 JSP 页面这些方法?)并且还能够加载静态内容(html、JS、CSS)
我现在设置的方式,我只能有一个或另一个,我不能同时工作。
我来解释一下:
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();
谢谢
**编辑:**
我遇到的问题是我的 HTTP servlet 的行为方式如下:
处理所有请求,不为资源处理程序留下任何内容,因此当脚本请求 .js 脚本时,它 returns 一个空的 html 页面。
这是一个例子:
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 });
在这个例子中,当使用没有主 servlet 的根处理程序时,它加载所有静态内容和 jsp 页面,但是当添加主 servlet 时,它不再加载任何静态内容,并响应所有对空 html 页面的静态内容的请求。
当您使用 servlet 时,servlet 链的末端总是有一个终止点。
可以是:
DefaultServlet
(使用完整 WebAppContext
时)
Default404Servlet
(当使用像 ServletContextHandler
这样更简单的设置时)
如果您只需要 ResourceHandler
来提供静态内容,请将 DefaultServlet
用于您自己的目的(它是更好的选择并且支持更多功能。例如范围请求、缓存、自动 gzip、内存映射文件服务等)
示例:
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);
}
}
}
我在为我的 Web 应用程序设置处理程序时遇到问题,我想要的是:让 HTTPServlet 使用 doGet 和 doPost 方法处理一些请求(如何从内部加载 JSP 页面这些方法?)并且还能够加载静态内容(html、JS、CSS)
我现在设置的方式,我只能有一个或另一个,我不能同时工作。
我来解释一下:
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();
谢谢
**编辑:**
我遇到的问题是我的 HTTP servlet 的行为方式如下: 处理所有请求,不为资源处理程序留下任何内容,因此当脚本请求 .js 脚本时,它 returns 一个空的 html 页面。 这是一个例子:
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 });
在这个例子中,当使用没有主 servlet 的根处理程序时,它加载所有静态内容和 jsp 页面,但是当添加主 servlet 时,它不再加载任何静态内容,并响应所有对空 html 页面的静态内容的请求。
当您使用 servlet 时,servlet 链的末端总是有一个终止点。
可以是:
DefaultServlet
(使用完整WebAppContext
时)Default404Servlet
(当使用像ServletContextHandler
这样更简单的设置时)
如果您只需要 ResourceHandler
来提供静态内容,请将 DefaultServlet
用于您自己的目的(它是更好的选择并且支持更多功能。例如范围请求、缓存、自动 gzip、内存映射文件服务等)
示例:
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);
}
}
}