本文介紹了如何配置Jetty處理程序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在為我的Web應用程序設置處理程序時遇到了一個問題,我想要的是:使用doGet和doPost方法的HTTPServlet處理一些請求(如何從這些方法中加載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腳本時,它返回一個空的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
用于您自己的目的(這是一個更好的選擇,而且還支持更多功能。例如范圍請求、緩存、自動壓縮、內存映射文件服務等)
示例:
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);
}
}
}
這篇關于如何配置Jetty處理程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,