1. 程式人生 > >Jetty實戰(1)之嵌入式執行Jetty

Jetty實戰(1)之嵌入式執行Jetty

Jetty最常用的一種用法是把Jetty嵌入到自己的Java應用程式中,此時Jetty作為一個後臺的Servlet容器執行,接受使用者的http請求,下面是一個最簡單的嵌入Jetty的用法。

1. 首先使用Maven建立一個java工程

  1. mvn archetype:generate -DgroupId=com.google.code.garbagecan.jettystudy -DartifactId=jettystudy -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  
2. 修改pom檔案,新增或修改編譯和依賴部分
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.google.code.garbagecan.jettystudy</groupId>
  5.     <artifactId>jettystudy</artifactId>
  6.     <packaging>jar</packaging
    >
  7.     <version>1.0-SNAPSHOT</version>
  8.     <name>jettystudy</name>
  9.     <url>http://maven.apache.org</url>
  10.     <build>
  11.         <plugins>
  12.             <plugin>
  13.                 <artifactId>maven-compiler-plugin</artifactId>
  14.                 <
    inherited>true</inherited>
  15.                 <configuration>
  16.                     <source>1.6</source>
  17.                     <target>1.6</target>
  18.                     <debug>true</debug>
  19.                 </configuration>
  20.             </plugin>
  21.         </plugins>
  22.     </build>
  23.     <dependencies>
  24.         <dependency>
  25.             <groupId>org.eclipse.jetty.aggregate</groupId>
  26.             <artifactId>jetty-all</artifactId>
  27.             <version>8.0.4.v20111024</version>
  28.             <type>jar</type>
  29.             <scope>provided</scope>
  30.         </dependency>
  31.         <dependency>
  32.             <groupId>junit</groupId>
  33.             <artifactId>junit</artifactId>
  34.             <version>3.8.1</version>
  35.             <scope>test</scope>
  36.         </dependency>
  37.     </dependencies>
  38. </project>
3. 建立一個Server類,用了啟動Jetty server,並且通過一個HelloHandler來處理瀏覽器傳送過來的請求;
  1. package com.google.code.garbagecan.jettystudy.sample1;  
  2. import org.eclipse.jetty.server.Server;  
  3. publicclass MyServer {  
  4.     publicstaticvoid main(String[] args) throws Exception {  
  5.         Server server = new Server(8080);  
  6.         server.setHandler(new HelloHandler());  
  7.         server.start();  
  8.         server.join();  
  9.     }  
  10. }  
4. 建立一個Handler類,用了處理所有客戶端的請求
  1. package com.google.code.garbagecan.jettystudy.sample1;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6. import org.eclipse.jetty.server.Request;  
  7. import org.eclipse.jetty.server.handler.AbstractHandler;  
  8. publicclass HelloHandler extends AbstractHandler {  
  9.     publicvoid handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)  
  10.             throws IOException, ServletException {  
  11.         response.setContentType("text/html;charset=utf-8");  
  12.         response.setStatus(HttpServletResponse.SC_OK);  
  13.         baseRequest.setHandled(true);  
  14.         response.getWriter().println("<h1>Hello World</h1>");  
  15.         response.getWriter().println("Request url: " + target);  
  16.     }  
  17. }  
5. 執行MyServer類,然後通過瀏覽器訪問http://localhost:8080/,可以看到“Hello World!”和請求的url。