使用Handler(嵌入Jetty学习二)
? ? Handler在Jetty中是一个非常重要的东西,Jetty内部实现了一些Handler,可以分为一下几类:
? ? 1.协调Handler:负责将request路由到其他Handler的Handler(如:HandlerConnection, ContextHandlerConnection)
? ??2.过滤Handler:负责向request中设置一些参数,然后再把请求转交给其他Handler(如:HandlerWapper, ContextHandler, SessionHandler)
? ? 3.生成Handler:负责生成响应的内容(如:ResourceHandler, ServletHandler)
?
? ? Handler的接口是这样的:
? ?HandlerList和HandlerConnection内部都可以设置若干Handler,?Handler按顺序一个接一个的执行。对于HandlerList而言,只要有一个Handler将请求标记为已处理,或抛出异常,Handler的调用就到此结束。而HandlerConnection则不会结束,一直调用到最后一个Handler。
?
? ? 下面以一个具体的例子来说明,需要注意HandlerList和HandlerConnection的区别,代码里有注释:
public class ManyHandlers {public static void main(String[] args) throws Exception {Server server = new Server(8080);// create the handlersHandler param = new ParamHandler();HandlerWrapper wrapper = new HandlerWrapper() {@Overridepublic void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {request.setAttribute("welcome", "Wylazy");super.handle(target, baseRequest, request, response);}};Handler hello = new HelloHandler();wrapper.setHandler(hello);Handler dft = new DefaultHandler();//HandlerList会依次调用每一个Handler,直到某个Handler将请求标记为已处理,即setHandled(true);HandlerList list = new HandlerList();list.setHandlers(new Handler[] { param, wrapper, dft });RequestLogHandler log = new RequestLogHandler();log.setRequestLog(new NCSARequestLog(File.createTempFile("demo", "log").getAbsolutePath()));//HandlerCollection会依次调用每一个Handler,即使请求已经被处理了HandlerCollection handlers = new HandlerCollection();handlers.setHandlers(new Handler[] { list, log });server.setHandler(handlers);server.start();server.join();}public static class ParamHandler extends AbstractHandler {public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {Map params = request.getParameterMap();if (params.size() > 0) {response.setContentType("text/plain");response.getWriter().println(new JSONObject(params));((Request) request).setHandled(true);}}}}?
?
参考:
http://zldeng1984.iteye.com/blog/1368376
http://wiki.eclipse.org/Jetty/Howto/Write_Jetty_Handler
http://wiki.eclipse.org/Jetty/Reference/Jetty_Architecture