Jetty - 嵌入式运行Servlet

时间:2022-09-23 19:32:13
一、说明
    在嵌入式Jetty中,有时候我们想运行一些的Servlet,此时就需要创建创建Context,然后让自己的Servlet运行在这些ServletContext中。

二、应用
    1、方式一

         首先创建一个ServletContextServer类,用来初始化web应用程序的Context,并且指定Servlet和Servlet匹配的url。这里指定了两个Servlet,分别是IndexServlet和UserServlet,并分别对应/test/index/indexServlet和/test/user/userServlet。

package com.test.main;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.test.servlet.IndexServlet;
import com.test.servlet.UserServlet;

public class Main {

public static void main(String[] args) throws Exception {

Server service = new Server();
//创建连接器
Connector connector = new SelectChannelConnector();
// 设置端口
connector.setPort(80);
// 设置host地址
connector.setHost("127.0.0.1");

service.setConnectors(new Connector[] { connector });

//ServletContext
ServletContextHandler context =
new ServletContextHandler(ServletContextHandler.SESSIONS);
//设置根路径
context.setContextPath("/test");

//设置servlet的url
context.addServlet(new ServletHolder(new IndexServlet()), "/index/indexServlet");
context.addServlet(new ServletHolder(new UserServlet()), "/user/userServlet");

service.setHandler(context);

service.setStopAtShutdown(true);
service.setSendServerVersion(true);

// 启动服务
service.start();
service.join();
}
}
    2、方式二
         除了上面的方式外,也可以创建两个Context,分别绑定到/index/indexServlet和/user/userServlet,如下:

package com.test.main;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.test.servlet.IndexServlet;
import com.test.servlet.UserServlet;

public class Main {

public static void main(String[] args) throws Exception {

Server service = new Server();
//创建连接器
Connector connector = new SelectChannelConnector();
// 设置端口
connector.setPort(80);
// 设置host地址
connector.setHost("127.0.0.1");

//context1
ServletContextHandler context1 =
new ServletContextHandler(ServletContextHandler.SESSIONS);
context1.setContextPath("/index");
context1.setClassLoader(Thread.currentThread().getContextClassLoader());
context1.addServlet(new ServletHolder(new IndexServlet()), "/indexServlet");

//context2
ServletContextHandler context2 =
new ServletContextHandler(ServletContextHandler.SESSIONS);
context2.setContextPath("/user");
context2.setClassLoader(Thread.currentThread().getContextClassLoader());
context2.addServlet(new ServletHolder(new UserServlet()), "/userServlet");

//contexts
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { context1, context2 });

service.setHandler(contexts);

service.setStopAtShutdown(true);
service.setSendServerVersion(true);

// 启动服务
service.start();
service.join();
}
}

三、参考

   http://blog.csdn.net/kongxx/article/details/7230080