阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Jetty使用教程

427次阅读
没有评论

共计 7668 个字符,预计需要花费 20 分钟才能阅读完成。

jetty 作为一款小型的 web 容器用处很大,因为其小巧强大,经常作为嵌入式的组件处理 http 交互。

Jetty 作为一个独立的 Servlet 引擎可以独立提供 Web 服务,但是它也可以与其他 Web 应用服务器集成,所以它可以提供基于两种协议工作,一个是 HTTP,一个是 AJP 协议,本文介绍 jetty 处理 http 请求的应用。实际上 Jetty 的工作方式非常简单,当 Jetty 接受到一个请求时,Jetty 就把这个请求交给在 Server 中注册的代理 Handler 去执行,如何执行你注册的 Handler,同样由你去规定,Jetty 要做的就是调用你注册的第一个 Handler 的 handle

下面自己对 jetty 的使用。

动起来:

1. 下载 jetty:http://www.eclipse.org/jetty/documentation/current/quick-start-getting-started.html#jetty-downloading

2. 在项目中引入包,如图:

Jetty 使用教程

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 特别适合处理聊天室之类的应用。

Jetty 服务器的安装配置详解 http://www.linuxidc.com/Linux/2015-09/123299.htm

使用 Jetty 作为嵌入式服务器 http://www.linuxidc.com/Linux/2013-07/86983.htm

Jetty 源码分析 http://www.linuxidc.com/Linux/2013-10/90986.htm

Jetty 安装学习并展示 http://www.linuxidc.com/Linux/2014-05/101993.htm

Jetty 在 Eclipse 中的安装 http://www.linuxidc.com/Linux/2013-10/90991.htm

Linux(RedHat 5.8) 下 安装 Jetty 部署 使用  http://www.linuxidc.com/Linux/2014-10/108342.htm

Jetty 的详细介绍 :请点这里
Jetty 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-04/129738.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计7668字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7991084
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...
告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

  告别 Notion 焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁” 引言 在数字笔记工...
星哥带你玩飞牛NAS-1:安装飞牛NAS

星哥带你玩飞牛NAS-1:安装飞牛NAS

星哥带你玩飞牛 NAS-1:安装飞牛 NAS 前言 在家庭和小型工作室场景中,NAS(Network Atta...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
安装并使用谷歌AI编程工具Antigravity(亲测有效)

安装并使用谷歌AI编程工具Antigravity(亲测有效)

  安装并使用谷歌 AI 编程工具 Antigravity(亲测有效) 引言 Antigravity...
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击 PHP-FPM(FastCGl Process M...
星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛 NAS 硬件 02:某鱼 6 张左右就可拿下 5 盘位的飞牛圣体 NAS 前言 大家好,我是星...