Jetty嵌入式开发

来源:互联网 发布:百度图片json接口 编辑:程序博客网 时间:2024/05/22 04:27
Jetty按照功能可以分为四个主个主要的部分,HttpServer, HttpContext,HttpHandler,HttpListener。


HttpServer的作用就是在一系列的监听器类和处理器类之间搭起了一个桥梁,有效的控制着消息在系统内的传递。HttpServer职责是接受从HttpListener传递过来的request(请求),HttpServer通过对request的Host(主机)或Path(路径)进行匹配,然后分发给相应的HttpContext(可以理解为一个web application)。

使用maven 创建一个Web项目,需要的jar包
POM.xml
<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.mapbar.jettyDemo</groupId >       <artifactId> jettyDemo</artifactId >       <packaging> war</packaging >       <version> 1.0</version >       <name> jettyDemo</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> commons-logging</groupId >                   <artifactId> commons-logging</artifactId >                   <version> 1.1.2</version >             </dependency>             <dependency>                   <groupId> org.mortbay.jetty</groupId >                   <artifactId> org.mortbay.jetty</artifactId >                   <version> 5.1.9</version >             </dependency>             <dependency>                   <groupId> org.mortbay.jetty</groupId >                   <artifactId> org.mortbay.jmx</artifactId >                   <version> 5.1.9</version >             </dependency>             <dependency>                   <groupId> javax.servlet</groupId >                   <artifactId> servlet-api</artifactId >                   <version> 2.4</version >                   <scope> provided</scope >             </dependency>             <dependency>                   <groupId> javax.servlet</groupId >                   <artifactId> jsp-api</artifactId >                   <version> 2.0</version >                   <scope> provided</scope >             </dependency>             <dependency>                   <groupId> tomcat</groupId >                   <artifactId> jasper-compiler</artifactId >                   <version> 5.5.15</version >                   <scope> provided</scope >             </dependency>             <dependency>                   <groupId> tomcat</groupId >                   <artifactId> jasper-compiler-jdt</artifactId >                   <version> 5.5.15</version >                   <scope> provided</scope >             </dependency>             <dependency>                   <groupId> tomcat</groupId >                   <artifactId> jasper-runtime</artifactId >                   <version> 5.5.15</version >                   <scope> provided</scope >             </dependency>       </dependencies>       <build>             <finalName> jettyDemo</finalName >       </build></project>

创建一个启动类
package com.mapbar.jetty;import org.mortbay.http.SocketListener;import org.mortbay.jetty.Server;public class StartJetty {       public static void main(String[] args) throws Exception {             //创建Jetty HttpServer对象            Server server = new Server();             //在端口8080上给HttpServer对象绑上一个listener,使之能够接收HTTP请求            SocketListener listener = new SocketListener();            listener.setPort(8090);            server.addListener(listener);             // 第一个参数为ContextPath,第二个webapp路径            server.addWebApplication( "/","./src/main/webapp" );             // 启动服务器            server.start();      }}
http://localhost:8090/  访问

启动过程:
  server 启动其它组件的顺序是:首先启动设置到 Server 的 Handler,通常这个 Handler 会有很多子 Handler,这些 Handler 将组成一个 Handler 链。Server 会依次启动这个链上的所有 Handler。接着会启动注册在 Server 上 JMX 的 Mbean,让 Mbean 也一起工作起来,最后会启动 Connector,打开端口,接受客户端请求,启动逻辑非常简单。 
原创粉丝点击