设为首页 收藏本站
查看: 1125|回复: 0

[经验分享] Jetty使用攻略

[复制链接]

尚未签到

发表于 2017-3-1 11:57:55 | 显示全部楼层 |阅读模式
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.在项目中引入包,如图:
DSC0000.png

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特别适合处理聊天室之类的应用。
参考:http://www.eclipse.org/jetty/documentation/current/

https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348793-1-1.html 上篇帖子: Jetty使用 下篇帖子: Jetty 的工作原理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表