【发布时间】:2015-03-21 05:48:30
【问题描述】:
我在为我的 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 脚本时,它会返回一个空的 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 });
在这个例子中,当使用没有 Main servlet 的 root handler 时,它会加载所有的静态内容和 jsp 页面,但是当添加 main servlet 时,它不再加载任何静态内容,并且响应所有对静态内容的请求一个空的 html 页面。
【问题讨论】: