分享

JETTY9(9.2.1)入门学习

 instl 2014-09-24

下载的jetty-distribution-9.2.1.v20140609,算是目前为止的最新版本,jetty的版本比较多,而且各种版本有一定的差异,感觉这点不如tomcat。

1.解压jetty到指定的目录,该目录就是$(JETTY_HOME)

   进入到该目录,运行java -jar start.jar,就可以启动jetty了。



2.运行jetty的demo-base

  cd ${JETTY_HOME}/demo_base 目录,运行 java -jar ../start.jar 就可以启动demo-base并看到jetty的欢迎界面了。


3.修改jetty运行端口

   (1)命令启动时指定端口号: java -jar start.jar jetty.port=8081


   (2)修改配置文件: 修改${JETTY_HOME}/start.d/http.ini中指定的端口号。

4. 部署web应用

    和tomcat相同,直接放到${JETTY_HOME}/webapps下面就行,看网上说还需要改什么设置,这里测试了一下,直接放上就可以启动。这里部署的是标准的web应用。如果文件夹或者war包名称是root,jetty视为根目录。


5.热部署

jettty可以通过监控目录的变化来部署应用,如果你往目录里面添加一个web应用,jetty的部署管理器(DM deployment manager)就会部署一个新的上下文。可以通过配置文件修改相关的属性,该文件的默认位置是${JETTY_HOME}/etc/jetty-deploy.xml

  1. <span style="font-size:14px;"><?xml version="1.0"?>  
  2. <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www./jetty/configure_9_0.dtd">  
  3. <Configure id="Server" class="org.eclipse.jetty.server.Server">  
  4.    
  5.   <Call name="addBean">  
  6.     <Arg>  
  7.       <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">  
  8.         <Set name="contexts">  
  9.           <Ref refid="Contexts" />  
  10.         </Set>  
  11.         <Call id="webappprovider" name="addAppProvider">  
  12.           <Arg>  
  13.             <New class="org.eclipse.jetty.deploy.providers.WebAppProvider">  
  14.               <Set name="monitoredDirName"><Property name="jetty.home" default="." />/webapps</Set>  
  15.               <Set name="defaultsDescriptor"><Property name="jetty.home" default="." />/etc/webdefault.xml</Set>  
  16.               <Set name="scanInterval">1</Set>  
  17.               <Set name="extractWars">true</Set>  
  18.             </New>  
  19.           </Arg>  
  20.         </Call>  
  21.       </New>  
  22.     </Arg>  
  23.   </Call>  
  24. </Configure></span>  

6.Maven和Jetty

使用嵌入式JETTY和MAVEN

MAVEN使用约定优于配置,所以最好是使用MAVEN推荐的工程结构。可以使用archetypes快速设置maven项目,现在为这个简单的例子使用手工建立工程结构。

  1. > mkdir JettyMavenHelloWorld  
  2. > cd JettyMavenHelloWorld  
  3. > mkdir -p src/main/java/org/example  

windows下可以使用md命令建立多级目录,在路径src/main/java/org/example/下建立HelloWorld.java类,内容如下:

  1. package org.example;  
  2.    
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import javax.servlet.ServletException;  
  6. import java.io.IOException;  
  7. import org.eclipse.jetty.server.Server;  
  8. import org.eclipse.jetty.server.Request;  
  9. import org.eclipse.jetty.server.handler.AbstractHandler;  
  10.    
  11. public class HelloWorld extends AbstractHandler  
  12. {  
  13.     public void handle(String target,  
  14.                        Request baseRequest,  
  15.                        HttpServletRequest request,  
  16.                        HttpServletResponse response)   
  17.         throws IOException, ServletException  
  18.     {  
  19.         response.setContentType("text/html;charset=utf-8");  
  20.         response.setStatus(HttpServletResponse.SC_OK);  
  21.         baseRequest.setHandled(true);  
  22.         response.getWriter().println("<h1>Hello World</h1>");  
  23.     }  
  24.    
  25.     public static void main(String[] args) throws Exception  
  26.     {  
  27.         Server server = new Server(8080);  
  28.         server.setHandler(new HelloWorld());  
  29.     
  30.         server.start();  
  31.         server.join();  
  32.     }  
  33. }  

建立POM描述文件,pom.xml描述文件描述了工程的名称和依赖,使用文本编辑器在JettyMavenHelloWorld目录下建立pom.xml,内容如下

  1. <project xmlns="http://maven./POM/4.0.0"  
  2.          xmlns:xsi="http://www./2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd">  
  4.    
  5.   <modelVersion>4.0.0</modelVersion>  
  6.   <groupId>org.example</groupId>  
  7.   <artifactId>hello-world</artifactId>  
  8.   <version>0.1-SNAPSHOT</version>  
  9.   <packaging>jar</packaging>  
  10.   <name>Jetty HelloWorld</name>  
  11.    
  12.   <properties>  
  13.       <!-- Adapt this to a version found on  
  14.            http://central./maven2/org/eclipse/jetty/jetty-maven-plugin/  
  15.         -->  
  16.       <jettyVersion>9.0.2.v20130417</jettyVersion>  
  17.   </properties>  
  18.    
  19.   <dependencies>  
  20.     <dependency>  
  21.       <groupId>org.eclipse.jetty</groupId>  
  22.       <artifactId>jetty-server</artifactId>  
  23.       <version>${jettyVersion}</version>  
  24.     </dependency>  
  25.   </dependencies>  
  26.    
  27.   <build>  
  28.     <plugins>  
  29.       <plugin>  
  30.         <groupId>org.codehaus.mojo</groupId>  
  31.         <artifactId>exec-maven-plugin</artifactId>  
  32.         <version>1.1</version>  
  33.         <executions>  
  34.           <execution><goals><goal>java</goal></goals></execution>  
  35.         </executions>  
  36.         <configuration>  
  37.           <mainClass>org.example.HelloWorld</mainClass>  
  38.         </configuration>  
  39.       </plugin>  
  40.     </plugins>  
  41.   </build>  
  42. </project>  
编译和运行HelloWorld.java类
  1. > mvn clean compile exec:java    
编译成功后,可以访问http://localhost:8080/会看到页面中打印出HelloWorld字样。






  1.   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多