`
king_tt
  • 浏览: 2142120 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Jetty使用入门

 
阅读更多

Jetty 是一个Web server/servletcontainer, 支持SPDY,WebSocket,OSGi,JMX,JNDI,JAAS。Jetty非常高效而且灵活,Google App Engine 选择了Jetty,而放弃了Tomcat,或是其他的服务器。

Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your application." What this means is that, putting an HTTP module into your application, rather than putting your application into an HTTP server.

Jetty的口号是:“不要把你的程序部署到Jetty里,而是把Jetty部署到你的程序里”,意味着,你可以把Jetty当成程序的一个HTTP模块放到你的程序里。

本文先通过一个简单的HelloWorld示例,展示了java应用中的Jetty是如何启动的;接着详细分析了Jetty的整体架构;最后展示了用Jetty启动一个标准的Java web app。

Hello World 示例

需要的jar包:

jetty-server-8.1.11.v20130520.jar
javax.servlet-3.0.0.v201112011016.jar
jetty-continuation-8.1.11.v20130520.jar
jetty-http-8.1.11.v20130520.jar
jetty-io-8.1.11.v20130520.jar
jetty-util-8.1.11.v20130520.jar

HelloWorldHandler 类:

复制代码
package edu.shao.jetty.sample;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorldHandler extends AbstractHandler {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("<h1>Hello World</h1>"); } }
复制代码

MyServer 类:

复制代码
package edu.shao.jetty.sample;

import org.eclipse.jetty.server.Server;

public class MyServer {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8081); 
        server.setHandler(new HelloWorldHandler()); 
        server.start(); 
        server.join();
    }
}
复制代码

运行main()函数,在浏览器内输入:http://localhost:8081/就可以看得结果。

Jetty架构

1、整体架构图:

The Jetty Server is the plumbing between a collection of Connectors that accept HTTP connections, and a collection of Handlers that service requests from the connections and produce responses, with the work being done by threads taken from a thread pool.(The concept of a Servlet itself is implemented by aServlet Handler. you can build a Jetty server using only connectors and handlers, without using Servlets.)

2、顶层类结构:

受JSR77规范的启发,Jetty的绝大多数的组件(Connector, Handler ,Buffer)都实现了LifeCycle接口。

3、Connectors:

The connectors represent the protocol handlers that accept connections, parse requests and generate responses. The different types of connectors available are based on the protocols, scheduling model and IO APIs used:

  1、SocketConnector - for few busy connections or when NIO is not available

  2、BlockingChannelConnector - for few busy connections when NIO is available

  3、SelectChannelConnector - for many mostly idle connections or asynchronous handling of Ajax requests

  4、SslSocketConnector - SSL without NIO

  5、SslSelectChannelConnector - SSL with non blocking NIO support

  6、AJPConnector - AJP protocol support for connections from apache mod_jk or mod_proxy_ajp

4、Handlers:

  

The Handler is the component that deals with received requests. Three styles of Handler:

  1、Coordinating Handlers - Handlers that route requests to other handlers (eg HandlerCollection, ContextHandlerCollection)
  2、Filtering Handlers - Handlers that augment a request and pass it on to other handlers (eg. HandlerWrapper, ContextHandler, SessionHandler)
  3、Generating Handlers - Handlers that produce content (eg ResourceHandler and ServletHandler)

重点Handler:

  1、The ServletHandler is a Handler that generates content by passing the request to any configured Filters and then to a Servlet mapped by a URI pattern.

  2、A WebAppContext combines handlers for security, session and servlets in a single unit that can be configured with aweb.xmldescriptor.

你可以顺序调用Handler,或者嵌套调用Handler,来处理请求的不同方面。

  

5、web应用

A WebAppContextsupports the standardized layout of a web application and configuration ofsession, security, listeners, filter, servlets and JSPvia aweb.xmldescriptor normally found in the WEB-INF directory of a webapplication.

把Jetty“部署”到Web应用中

1、开发时的部署示例:

这种部署方式还有一个诱人的特性:项目启动后,如果某个类没有被加载到内存中,对这个类的修改在下次该类被调用时就会生效,而不用重启动项目;对JSP的修改,任何时候都会在下次被调用时生效,而不用重启项目。这将给开发web应用带来极大的便利。

这是用Maven构件的Java Web App项目,项目结构如下:

  

WebappStart 类:

复制代码
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class WebappStart {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8082);
         
        WebAppContext context = new WebAppContext();
        context.setResourceBase("./src/main/webapp");
        context.setDescriptor("./src/main/webapp/WEB-INF/web.xml");
        context.setContextPath("/test2");
        context.setParentLoaderPriority(true);
 
        server.setHandler(context);
 
        server.start();
        server.join();

    }

}
复制代码

启动main()函数,整个web项目就启动了。

我们可以体验到,把Jetty嵌入到Web项目中,作为Web Server,十分便利、灵活,并且相比其他服务器软件要高效,是开发Web应用的首选WebServer。

2、用Jetty部署war包

此部分稍后撰写。

分享到:
评论

相关推荐

    jetty快速入门与嵌入使用

    jetty快速入门与嵌入使用,简单、易懂,轻松学习!

    Jetty6入门教程

    资源名称:Jetty6入门教程资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

    jetty 入门实例

    jetty 超轻松入门,按步奏就可以学习,下载就可以运行

    jetty入门

    NULL 博文链接:https://jose-bing.iteye.com/blog/1240881

    Jetty入门学习资料

    Jetty 是一个开源的servlet容器,它为...Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。

    jetty6 嵌入式使用

    引入到eclipse可以直接使用Starter类运行。 包含必备jar包:jetty-6.1.26.jar,jetty-util-6.1.26,servlet的相关jar包。 jetty入门级源代码。

    Jetty中文手册

    Jetty入门(视频) 下载和安装Jetty 如何安装一个Jetty包 如何配置Jetty – 主要文档 如何运行Jetty 用JConsole监控Jetty 如何使用Jetty开发 Jetty HelloWorld教程 Jetty和Maven HelloWorld教程 Jetty(6)入门 ...

    Jetty Demo

    Hello Jetty 入门简单实例!可直接运行!

    jetty入门使用

    NULL 博文链接:https://smallbee.iteye.com/blog/1596501

    jetty嵌入项目实战

    里面包含的有jetty嵌入开发的全部jar包,还有一个jetty实战项目,非常适合刚入门的童鞋观看

    jetty-seed:使用 Maven、Jetty 和 Jersey 的入门代码

    使用 Maven、Jetty 和 Jersey 的入门代码 使用 maven-shade-plugin 构建一个带有依赖项的胖 jar 使用单一资源在 8080 上运行码头服务器 建造 安装 跑步 java -jar target/jetty-app-{{version}}.jar 测试资源

    Jetty 核心架构

    jetty7 核心架构 细致分析 入门到精通

    Intellij Idea使用入门

    Intellij Idea使用入门,包括如何在web项目里集成maven、svn及使用jetty

    maven的入门使用

    maven入门使用 maven创建web项目 maven生成eclipse项目 maven+jetty的使用 jetty在eclipse的配置和使用

    maven&struts2&jetty&tomcat

    该包适用于maven新手入门,因为作者本身也是一员maven新手,里面包括了maven的下载包,插件包,插件的配置,环境变量的设置,maven项目建立的详解及运行,里面集成了jetty,tomcat,struts2,当然还包括了直接运行jar包...

    J2EE入门级指导书

    J2EE入门级指导书

    Maven使用实战-从入门到精通实践【张振华.Jack】

    Maven使用实战-从入门到精通实践【张振华.Jack】 Maven简介、Maven主要做什么用? 项目对象模型(POM) 坐标(Coordinates)、打包方式(Packaging)、项目描述 项目子父级关系(parent) 依赖管理(dependency),冲突了...

    JettyTest.rar

    JettyTest.rar中介绍了jetty的入门案例,可以快速上手jetty,不好勿喷

    JAX-RS入门jar包集合

    使用到的外部jar包有(必须的部分,需要加到Web容器中) •neethi-3.0.2.jar •jsr311-api-1.1.1.jar •cxf-bundle-2.6.0.jar 使用到的外部jar包有(可选的部分,当且仅当作为一个独立的application运行时) ...

Global site tag (gtag.js) - Google Analytics