Jetty嵌入式开发

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,打开端口,接受客户端请求,启动逻辑非常简单。 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值