將Jetty做為內嵌的服務器使用
將jetty做為內嵌的服務器使用
Jetty的優良特點在于,它可以嵌入到任意的java程序中。并且使用jetty可以快速的開發自己的應用服務器。
1、準備開發嵌入式的jetty服務器jar包
//以下這些包,在jetty_home\lib目錄下都可以找到:
jetty-continuation-
jetty-http-
jetty-io-
jetty-security-
jetty-server-
jetty-servlet-
jetty-util-
jetty-webapp-
jetty-xml-
//以下是Servlet項目所需要的包,在tomcat_home\lib的安裝目錄下都可以找到
servlet-api-2.5.jar
jsp-api.jar
jasper.jar
jasper-el.jar
el-api.jar
ecj-
//以下在tomcat_home\bin目錄下可以找到,雖然它是以tomcat命名,但它是一個公共包,主要是用于服務器的日志輸出
tomcat-juli.jar
2、書寫第一個jetty服務器
以下代碼,創建了一個jetty服務器,并發布了一兩個Servlet。且設置支持Session。
import org.eclipse.jetty.server.Server; //此類是jetty的服務器類,用于指定綁定的服務器和端口。
import org.eclipse.jetty.server.SessionManager; //此類用于管理服務器的Session
import org.eclipse.jetty.server.session.HashSessionManager; //此類是SessionManager的子類,用hash算法生成Session
import org.eclipse.jetty.server.session.SessionHandler; //此類用于將SessionManager注冊到服務器
import org.eclipse.jetty.servlet.ServletContextHandler; //此類用于管理Servlet,可以管理多個Servlet.
import org.eclipse.jetty.servlet.ServletHolder; //此類用于將Servlet注冊到服務器
import org.eclipse.jetty.webapp.WebAppContext; //此類,可以用于加載任意一個JavaEE項目。后面會用到
以下是訪問內部的代碼,類名請你自己添加:
@Test
public void jettyServer2() throws Exception{
Server server = new Server(9090);//聲明服務器
ServletContextHandler hand = new ServletContextHandler();//聲明ServletContextHandler
hand.addServlet(new ServletHolder(new HttpServlet() { //分別添加兩個Servlet
public void doGet(HttpServletRequest req,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello World第二個</h1>");
HttpSession s = req.getSession();
String name = (String) s.getAttribute("name");
response.getWriter().println("<h1>Session is:</h1>"+s+","+name);
response.getWriter().print("<br/>"+this.getServletContext().getRealPath("/"));
}
}), "/a");
hand.addServlet(new ServletHolder(new HttpServlet() {
public void doGet(HttpServletRequest req,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello World第一個</h1>");
HttpSession s = req.getSession();
s.setAttribute("name", "Jack");
response.getWriter().print("<br/><a href='a'>去第二個</a>");
}
}), "/");
//設置內嵌的jetty支持session.默認情況下不支持session
SessionManager sm = new HashSessionManager();
hand.setSessionHandler(new SessionHandler(sm));
server.setHandler(hand);//設置
server.start();//啟動
server.join();
}
3、使用jetty的管理器管理一個完整的JavaEE應用
Jetty可以非常方便的管理一個JavaEE應用程序。關鍵點就在于使用WebAppContext來發布一個JavaEE應用:
/**
* 指定一個現有的項目發布,方式一。
* 以下情況下,默認支持容器的所有對象如:
* request,session,application,等
*/
public static void main(String[] args) throws Exception {
String webapp = "D:\\programfiles\\MyEclipse10\\wk3\\day04\\WebRoot";//聲明項目所在的目錄
Server server = new Server(80); //聲明端口
WebAppContext context = new WebAppContext(); //聲明上下文對象
context.setDescriptor(webapp + "/WEB-INF/web.xml"); //指定web.xml文件,可選
context.setResourceBase(webapp); //設置項目路徑
context.setContextPath("/"); //設置上下文根,可以任意的值
server.setHandler(context); //設置句柄
server.start(); //啟動
server.join();
}
也可以使用以下方式發布:
/**
* 指定一個現有的項目發布,方式二
* @throws Exception
*/
@Test
public void jettyServer() throws Exception{
String webapp = "D:\\programfiles\\MyEclipse10\\wk3\\day04\\WebRoot";
Server server = new Server(80);
WebAppContext context = new WebAppContext(webapp,"/abc");
server.setHandler(context);
server.start();
server.join();
}