jetty作为一款小型的web容器用处很大,因为其小巧强大,经常作为嵌入式的组件处理http交互。
Jetty 作为一个独立的 Servlet 引擎可以独立提供 Web 服务,但是它也可以与其他 Web 应用服务器集成,所以它可以提供基于两种协议工作,一个是 HTTP,一个是 AJP 协议,本文介绍jetty处理http请求的应用。 实际上 Jetty 的工作方式非常简单,当 Jetty 接受到一个请求时,Jetty 就把这个请求交给在 Server 中注册的代理 Handler 去执行,如何执行你注册的 Handler,同样由你去规定,Jetty 要做的就是调用你注册的第一个 Handler 的 handle
下面自己对jetty的使用。
动起来:
2.在项目中引入包,如图:
3.写一个Servlet处理类。
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String username= request.getParameter( "username");
String password= request.getParameter( "password");
response.setContentType( "text/html;charset=utf-8");
//返回html 页面
response.getWriter().println( "<html>");
response.getWriter().println( "<head>");
response.getWriter().println( "<title>登录信息</title>" );
response.getWriter().println( "</head>");
response.getWriter().println( "<body>");
response.getWriter().println( "欢迎【" + username + "】用户登录成功!!!" );
response.getWriter().println( "</body>");
response.getWriter().println( "</html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
//doGet(request,response);
response.sendRedirect( "/web-inf/jsp/default.jsp");
}
}
4.设置jetty启动项
public class JettyServer {
public static void main(String[] args) throws Exception {
Server server= new Server(8080);
ServletContextHandler context= new ServletContextHandler(ServletContextHandler.SESSIONS );
context.setContextPath( "/");
server.setHandler( context);
context.addServlet( new ServletHolder( new LoginServlet()),"/here");
server.start();
server.join();
}
}
5.启动并输入地址:
http://localhost:8080/here
至此,jetty简单运行成功。
在springMVC中使用jetty
1.引入pom文件:
<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.lango.test</ groupId>
< artifactId> springMvcJetty</ artifactId>
< packaging> war</ packaging>
< version> 0.0.1-SNAPSHOT</ version>
< name> springMvcJetty Maven Webapp</ 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 >org.eclipse.jetty </groupId >
<artifactId >jetty-io</artifactId >
<version >8.1.8.v20121106 </version >
</dependency >
<dependency >
<groupId >org.eclipse.jetty </groupId >
<artifactId >jetty-io</artifactId >
<version >8.1.8.v20121106 </version >
</dependency >
<dependency >
<groupId >org.eclipse.jetty </groupId >
<artifactId >jetty-webapp</artifactId >
<version >8.1.8.v20121106 </version >
</dependency >
<dependency >
<groupId >org.eclipse.jetty </groupId >
<artifactId >jetty-server</ artifactId>
<version >8.1.8.v20121106 </version >
</dependency >
<dependency >
<groupId >org.eclipse.jetty </groupId >
<artifactId >jetty-servlet</artifactId >
<version >8.1.8.v20121106 </version >
</dependency >
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring-context </artifactId >
<version >4.1.4.RELEASE </version >
</dependency >
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring- jms</ artifactId>
<version >4.1.6.RELEASE </version >
</dependency >
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring-test </artifactId >
<version >4.1.4.RELEASE </version >
</dependency >
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring-web </artifactId >
<version >4.1.4.RELEASE </version >
</dependency >
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring- webmvc</ artifactId>
<version >4.1.4.RELEASE </version >
</dependency >
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring-context-support </artifactId >
<version >4.1.4.RELEASE </version >
</dependency >
</ dependencies>
< build>
<finalName >springMvcJetty </finalName >
</ build>
</project>
2.配置jetty服务器:
public class jettyT {
public static void main(String[] args) throws Exception {
Server server= new Server(8080);
ServletContextHandler context= new ServletContextHandler(ServletContextHandler.SESSIONS );
context.setContextPath( "/");
//使用spring的Servlet处理
DispatcherServlet servlet= new DispatcherServlet();
servlet.setContextConfigLocation( "classpath*:springMvcJetty/spring-jetty.xml" );
context.addServlet( new ServletHolder(servlet), "/");
HandlerList handlers= new HandlerList();
handlers.addHandler( context);
server.setHandler( handlers);
ThreadPool pool = new ExecutorThreadPool(
Executors. newCachedThreadPool());
server.setThreadPool( pool);
Connector connector= new SelectChannelConnector();
connector.setPort(8080); //此处要与前面设置的 jetty端口一致。
server.setConnectors( new Connector[]{ connector});
server.start();
server.join();
}
}
3.其中的spring-jetty.xml配置:
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:mvc= "http://www.springframework.org/schema/mvc"
xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:p= "http://www.springframework.org/schema/p"
xsi:schemaLocation= "http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
" >
<!-- 注解扫描包 -->
<context:annotation-config />
<context:component-scan base-package ="springMvc"
use-default-filters= "false">
<context:include-filter type ="annotation" expression= "org.springframework.stereotype.Controller" />
</context:component-scan >
<!-- 开启注解 -->
<mvc:annotation-driven >
</mvc:annotation-driven >
</beans>
4.写一个controller:
@Controller
@RequestMapping("/home" )
public class Home {
@RequestMapping("/index")
@ResponseBody
public String Index(HttpServletRequest request){
String name= request.getParameter( "name");
return name+ "你好";
}
}
在浏览器中访问http://localhost:8080/home/index,就可以了。
扩展: Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求。 Continuations 机制维护一个队列,当请求在suspend方法之后就加入队列,等到超时或者调用 resume方法时,在交由线程处理。这使得jetty特别适合处理聊天室之类的应用。
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
- 微软MVP攻略 (如何成为MVP?一个SQL Server MVP的经验之谈)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 初衷 什么是微软MVP? 成为微软MVP的条件? 如何成为微软MVP? (一) 申请时间划分 (二) 前期准备 (三) ...
- Windows下LATEX排版论文攻略—CTeX、JabRef使用介绍
Windows下LATEX排版论文攻略—CTeX.JabRef使用介绍 一.工具介绍 TeX是一个很好排版工具,在学术界十分流行,特别是数学.物理学和计算机科学界. CTeX是TeX中的一个版本,指的 ...
- linux下安装apache与php;Apache+PHP+MySQL配置攻略
1.apache 在如下页面下载apache的for Linux 的源码包 http://www.apache.org/dist/httpd/; 存至/home/xx目录,xx是自建文件 ...
- 生成 PDF 全攻略【2】在已有PDF上添加内容
项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...
- Java数组技巧攻略
Java数组技巧攻略 0. 声明一个数组(Declare an array) String[] aArray = new String[5]; String[] bArray = {" ...
- BZOJ3252: 攻略
Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景 ...
- [经验] Win7减肥攻略(删文件不删功能、简化优化系统不简优化性能)
[经验] Win7减肥攻略(删文件不删功能.简化优化系统不简优化性能) ☆心梦无痕☆ 发表于 2014-1-24 11:15:04 https://www.itsk.com/thread-316471 ...
- 从小工到专家 ——读《Java程序员职场全攻略》有感
从小工到专家 ——读<Java程序员职场全攻略>有感 <Java程序员职场全攻略>是以故事的形式,向读者介绍Java程序员的职场经验.作者牛开复在北京从事软件开发,已经是一 ...
随机推荐
- HTML+AngularJS+Groovy如何实现登录功能
AngularJS是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS核心特性有:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入等.AngularJS认为声明 ...
- 支持10种格式的 HTML 表格导出 jQuery 插件
HTML 表格导出 jQuery 插件可以帮助用户导出 HTML 表格到 JSON.XML.PNG.CSV.TXT.SQL.MS-Word.MS-Excel.MS-PowerPoint 和 PDF 格 ...
- ie7下<a></a>标签不反应
view中: <a href="Trading?id=@dr["id"]"> <div class="sy_img_div" ...
- Sap 常用Function 说明
函数名 描述 SD_VBAP_READ_WITH_VBELN 根据销售订单读取表vbap中的信息EDIT_LINES 把READ_TEXT返回的LINES中的行按照TDFORMAT=“*”重新组织VI ...
- 全信号高清DVI编码器|上海视涛科技
高清DVI编码器(E700)简介 高清DVI编码器是上海视涛科技出品的高性能全信号DVI编码产品.该DVI编码器是上海视涛科技完全自主研发,并适用于DVI信号的编码采集及网络传输的专用硬件设备.可兼容 ...
- Maven版本与JDK版本
http://maven.apache.org/docs/history.html
- sharepoint 开发相关工具总结
1.CAML Designer 2013 开发caml用 http://biwug-web.sharepoint.com/SitePages/Caml_designer.aspx 2.SharePoi ...
- JSON数据解析 基础知识及链接收集
JSON数据解析学习 JSON介绍 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 是存储和交换文本信息的语法.类似 XML.但是JSON 比 ...
- laravel的一些坑
1.laravel 本身的性能不行,对高性能服务器,需要使用lumen 2. {{$url}} 默认会执行 htmlentities ,进行转意义,如果不需要转义可直接使用 php的echo 或者 { ...
- 小技巧,如何在Label中显示图片
这个需求其实是有的,比如QQ聊天界面里面发送的信息,可以用label来显示文字(也可以用button显示),但是有时候用户可能会发送图片.如果能让Label遇到文字就显示文字,遇到图片就显示图片就好了 ...