1. 程式人生 > >嵌入式 Jetty 9.0+ 測試demo

嵌入式 Jetty 9.0+ 測試demo

pom:

<!-- jetty dependecies -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.0.0.v20130308</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.0.0.v20130308</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-continuation</artifactId>
            <version>9.0.0.v20130308</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-jsp</artifactId>
            <version>9.0.0.v20130308</version>
        </dependency>
        <!-- jetty dependecies -->


demo:

package jetty.c1;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/10/31.
 */
public 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 "+request.getAttribute("welcome")+"</h1><div style=\"height:500px;background-image: url(data/nm.png);\">");
    }

    public static void main(String[] args) throws Exception
    {
        Server server = new Server();
        // 設定在JVM退出時關閉Jetty的鉤子。
        server.setStopAtShutdown(true);
        //server.setHandler(new HelloHandler());

	//設定多個connector繫結多個埠
        ServerConnector connector1 = new ServerConnector(server);
        connector1.setPort(8080);

        ServerConnector connector2 = new ServerConnector(server);
        connector2.setPort(9090);

        server.setConnectors(new Connector[] { connector1, connector2 });

        //資源目錄(本地路徑對映) 列出檔名
        ResourceHandler resourceHandler = new ResourceHandler();

        resourceHandler.setDirectoriesListed(true);
        resourceHandler.setResourceBase("D:\\360Downloads");
        resourceHandler.setStylesheet("");

        //設定靜態目錄訪問contextPath(對映到 上述 資源目錄中,即訪問 localhost:4080/data/a.jpg = 訪問 D:\\360Downloads\\a.jpg)
        ContextHandler staticContextHandler = new ContextHandler();
        staticContextHandler.setContextPath("/data");
        //staticContextHandler.setContextPath("/files");
        staticContextHandler.setHandler(resourceHandler);

        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        servletContextHandler.setContextPath("/");

        /**可在下面依次新增多個servlet**/
        servletContextHandler.addServlet(new ServletHolder(new MyTestServlet("show")), "/show");
        servletContextHandler.addServlet(new ServletHolder(new MyTestServlet("display")), "/display");
        //servletContextHandler.addServlet(new ServletHolder(new SpuSimilarityServlet()), "/spu");
        //servletContextHandler.addServlet(new ServletHolder(new SpuResultServlet()), "/search");
        //	servletContextHandler.addServlet(new ServletHolder(new AdminServlet()), "/db");

        //設定request等 回話值
        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);
            }
        };

        HandlerList handlers = new HandlerList();
        handlers.addHandler(wrapper);
        handlers.addHandler(servletContextHandler);
        handlers.addHandler(staticContextHandler);
        handlers.addHandler(new HelloHandler());
        server.setHandler(handlers);

        server.start();
        server.join();
    }
}


package jetty.c1;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */
public class MyTestServlet extends HttpServlet {

    private String msg = "init";

    public MyTestServlet(String msg) {
        this.msg = msg;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        List<String> list = new ArrayList<String>();

        list.add("A");
        list.add("中國");
        list.add("外國");
        list.add(msg);

        Gson g = new Gson();

        //resp.setContentType("charset=utf-8");
        ServletOutputStream out = resp.getOutputStream();
        String json = g.toJson(list);
        out.write(json.getBytes());
        out.flush();
        out.close();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doDelete(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doPut(req, resp);
    }

}

實際效果:

localhost:8080/9090

localhost:8080/show


http://localhost:8080/data/  檔案目錄展示: