jetty启动(server-connector-handle-start-join)

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.RequestLogHandler;

public class ManyHandlers {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        // create the handlers
        Handler param = new ParamHandler();
        Handler testHandler = new TestHandler();
        HandlerWrapper wrapper = new HandlerWrapper() {
            @Override
            public 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[]{testHandler, param, wrapper, dft});
        server.setHandler(list);

        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(params);
                baseRequest.setHandled(true);
            }
            //如果不设置该属性,则还会进入下一个handler
//          baseRequest.setHandled(true);
        }
    }
    public static class TestHandler extends AbstractHandler {

        @Override
        public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
            String requestURI = httpServletRequest.getRequestURI();
            System.out.println(requestURI);
            if (requestURI.contains("/test")) {
                httpServletResponse.setContentType("application/json");
                httpServletResponse.setCharacterEncoding("utf-8");
                httpServletResponse.getWriter().println("测试成功");
                request.setHandled(true);
            }
        }
    }
    public static class HelloHandler extends AbstractHandler {
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            response.getWriter().println("<h1>Hello World1</h1>");
            response.getWriter().println("Request url: " + target);
        }
    }

}

最后访问localhost:8080

 

posted @ 2018-08-31 11:29  奋小斗g  阅读(321)  评论(0编辑  收藏  举报