jetty作為一款小型的web容器用處很大,因為其小巧強大,經常作為嵌入式的組件處理http交互。 Jetty 作為一個獨立的 Servlet 引擎可以獨立提供 Web 服務,但是它也可以與其他 Web 應用伺服器集成,所以它可以提供基於兩種協議工作,一個是 HTTP,一個是 AJP 協議,本文介紹 ...
jetty作為一款小型的web容器用處很大,因為其小巧強大,經常作為嵌入式的組件處理http交互。 Jetty 作為一個獨立的 Servlet 引擎可以獨立提供 Web 服務,但是它也可以與其他 Web 應用伺服器集成,所以它可以提供基於兩種協議工作,一個是 HTTP,一個是 AJP 協議,本文介紹jetty處理http請求的應用。 實際上 Jetty 的工作方式非常簡單,當 Jetty 接受到一個請求時,Jetty 就把這個請求交給在 Server 中註冊的代理 Handler 去執行,如何執行你註冊的 Handler,同樣由你去規定,Jetty 要做的就是調用你註冊的第一個 Handler 的 handle 下麵自己對jetty的使用。 動起來: 1.下載jetty:http://www.eclipse.org/jetty/documentation/current/quick-start-getting-started.html#jetty-downloading 2.在項目中引入包,如圖: 3.寫一個Servlet處理類。 public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String username= request.getParameter( "username"); String password= request.getParameter( "password"); response.setContentType( "text/html;charset=utf-8"); //返回html 頁面 response.getWriter().println( "<html>"); response.getWriter().println( "<head>"); response.getWriter().println( "<title>登錄信息</title>" ); response.getWriter().println( "</head>"); response.getWriter().println( "<body>"); response.getWriter().println( "歡迎【" + username + "】用戶登錄成功!!!" ); response.getWriter().println( "</body>"); response.getWriter().println( "</html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { //doGet(request,response); response.sendRedirect( "/web-inf/jsp/default.jsp"); } } 4.設置jetty啟動項 public class JettyServer { public static void main(String[] args) throws Exception { Server server= new Server(8080); ServletContextHandler context= new ServletContextHandler(ServletContextHandler.SESSIONS ); context.setContextPath( "/"); server.setHandler( context); context.addServlet( new ServletHolder( new LoginServlet()),"/here"); server.start(); server.join(); } } 5.啟動並輸入地址: http://localhost:8080/here 至此,jetty簡單運行成功。 在springMVC中使用jetty 1.引入pom文件: <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> < modelVersion>4.0.0</modelVersion > < groupId> com.lango.test</ groupId> < artifactId> springMvcJetty</ artifactId> < packaging> war</ packaging> < version> 0.0.1-SNAPSHOT</ version> < name> springMvcJetty Maven Webapp</ name> < url> http://maven.apache.org</ url> < dependencies> <dependency > <groupId >junit</groupId> <artifactId >junit</artifactId> <version >3.8.1 </version > <scope >test </scope > </dependency > <dependency > <groupId >org.eclipse.jetty </groupId > <artifactId >jetty-io</artifactId > <version >8.1.8.v20121106 </version > </dependency > <dependency > <groupId >org.eclipse.jetty </groupId > <artifactId >jetty-io</artifactId > <version >8.1.8.v20121106 </version > </dependency > <dependency > <groupId >org.eclipse.jetty </groupId > <artifactId >jetty-webapp</artifactId > <version >8.1.8.v20121106 </version > </dependency > <dependency > <groupId >org.eclipse.jetty </groupId > <artifactId >jetty-server</ artifactId> <version >8.1.8.v20121106 </version > </dependency > <dependency > <groupId >org.eclipse.jetty </groupId > <artifactId >jetty-servlet</artifactId > <version >8.1.8.v20121106 </version > </dependency > <dependency > <groupId >org.springframework </groupId > <artifactId >spring-context </artifactId > <version >4.1.4.RELEASE </version > </dependency > <dependency > <groupId >org.springframework </groupId > <artifactId >spring- jms</ artifactId> <version >4.1.6.RELEASE </version > </dependency > <dependency > <groupId >org.springframework </groupId > <artifactId >spring-test </artifactId > <version >4.1.4.RELEASE </version > </dependency > <dependency > <groupId >org.springframework </groupId > <artifactId >spring-web </artifactId > <version >4.1.4.RELEASE </version > </dependency > <dependency > <groupId >org.springframework </groupId > <artifactId >spring- webmvc</ artifactId> <version >4.1.4.RELEASE </version > </dependency > <dependency > <groupId >org.springframework </groupId > <artifactId >spring-context-support </artifactId > <version >4.1.4.RELEASE </version > </dependency > </ dependencies> < build> <finalName >springMvcJetty </finalName > </ build> </project> 2.配置jetty伺服器: public class jettyT { public static void main(String[] args) throws Exception { Server server= new Server(8080); ServletContextHandler context= new ServletContextHandler(ServletContextHandler.SESSIONS ); context.setContextPath( "/"); //使用spring的Servlet處理 DispatcherServlet servlet= new DispatcherServlet(); servlet.setContextConfigLocation( "classpath*:springMvcJetty/spring-jetty.xml" ); context.addServlet( new ServletHolder(servlet), "/"); HandlerList handlers= new HandlerList(); handlers.addHandler( context); server.setHandler( handlers); ThreadPool pool = new ExecutorThreadPool( Executors. newCachedThreadPool()); server.setThreadPool( pool); Connector connector= new SelectChannelConnector(); connector.setPort(8080); //此處要與前面設置的 jetty埠一致。 server.setConnectors( new Connector[]{ connector}); server.start(); server.join(); } } 3.其中的spring-jetty.xml配置: <?xml version="1.0" encoding= "UTF-8"?> <