zoukankan      html  css  js  c++  java
  • Jetty初探

    一、在jetty中部署web应用

    Jetty 和 Tomcat 一样都是一个web server的container, 用户可以在里面 deploy 自己的 war 包,然后启动 Jetty, 进而通过浏览器去访问你的 web content.

    从jetty官网下载jetty 9的压缩包,并解压到本地。假设jetty的路径是D:jetty-9, 在CMD里运行

    > cd D:jetty-9demo-base
    > java -jar ..start.jar
    2015-06-04 10:50:44.806:INFO::main: Logging initialized @334ms
    2015-06-04 10:50:44.858:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended.  See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html
    2015-06-04 10:50:44.995:INFO:oejs.Server:main: jetty-9.3.0.v20150601
    2015-06-04 10:50:45.012:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///opt/jetty-distribution-9.3.0.v20150601/webapps/] at interval 1
    2015-06-04 10:50:45.030:INFO:oejs.ServerConnector:main: Started ServerConnector@19dfb72a{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
    2015-06-04 10:50:45.030:INFO:oejs.Server:main: Started @558ms

    jetty 被启动,这时便可以访问http://localhost:8080去访问demo相关的内容了。Jetty 9中一项重大的改动就是允许一个server实例的内容与jetty自身的distribution隔离。用户在创建web应用时先创建一个新的Jetty Base.

    > JETTY_BASE=/tmp/mybase
    > mkdir $JETTY_BASE
    > cd $JETTY_BASE
    > java -jar $JETTY_HOME/start.jar
    WARNING: Nothing to start, exiting ...
    
    Usage: java -jar start.jar [options] [properties] [configs]
           java -jar start.jar --help  # for more information
    
    > java -jar $JETTY_HOME/start.jar --add-to-startd=http,deploy
    INFO: server          initialised (transitively) in ${jetty.base}/start.d/server.ini
    INFO: http            initialised in ${jetty.base}/start.d/http.ini
    INFO: security        initialised (transitively) in ${jetty.base}/start.d/security.ini
    INFO: servlet         initialised (transitively) in ${jetty.base}/start.d/servlet.ini
    INFO: webapp          initialised (transitively) in ${jetty.base}/start.d/webapp.ini
    INFO: deploy          initialised in ${jetty.base}/start.d/deploy.ini
    MKDIR: ${jetty.base}/webapps
    INFO: Base directory was modified
    > cp $JETTY_HOME/demo-base/webapps/async-rest.war webapps/ROOT.war
    > java -jar $JETTY_HOME/start.jar
    2015-06-04 11:10:16.286:INFO::main: Logging initialized @274ms
    2015-06-04 11:10:16.440:INFO:oejs.Server:main: jetty-9.3.0.v20150601
    2015-06-04 11:10:16.460:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///tmp/mybase/webapps/] at interval 1
    2015-06-04 11:10:16.581:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION!
    2015-06-04 11:10:16.589:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
    2015-06-04 11:10:16.628:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@1a407d53{/,[file:///tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-4510228025526425427.dir/webapp/, jar:file:///tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-4510228025526425427.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.3.0.v20150601.jar!/META-INF/resources],AVAILABLE}{/ROOT.war}
    2015-06-04 11:10:16.645:INFO:oejs.ServerConnector:main: Started ServerConnector@3abbfa04{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
    2015-06-04 11:10:16.646:INFO:oejs.Server:main: Started @634ms

    二、在java中部署jetty

    Jetty的口号是"Don't deploy your application in Jetty, deploy Jetty in your application",这也是Jetty与Tomcat最大的不同。在jetty中部署应用其实可以算是jetty的附带功能,jetty真正为人们所广泛应用的是deploy jetty in application.

    下面是java中部署jetty的helloworld。

    程序入口SimplestServer.main

    package com.mustone;
    
    import org.eclipse.jetty.server.Server;
    
    public class SimplestServer {
    
        public static void main(String[] args) throws Exception {
        Server server = new Server(8081);
            server.setHandler(new HelloHandler());
    
            server.start();
            server.join();
    
        }
    }
    package com.mustone;
    
    import org.eclipse.jetty.server.Request;
    import org.eclipse.jetty.server.handler.AbstractHandler;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class HelloHandler extends AbstractHandler {
        public void handle(String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response)
        throws IOException {
            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            response.getWriter().println("<h1>Hello World</h1>");
        }
    }

    运行SimplestServer, web server就启起来了

    2016-03-10 03:05:42.974:INFO:oejs.Server:jetty-7.6.17.v20150415
    2016-03-10 03:05:43.043:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8081

    这时访问http://localhost:8081就可以看到Hello World的字样。

  • 相关阅读:
    单例模式
    json 格式
    axios 获取不到数据错误
    sprint test 添加事务回滚机制
    springboot An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    spring boot 启动之后404
    废了
    tomcat 部署项目到服务器
    Druid 介绍及配置
    jq 全选
  • 原文地址:https://www.cnblogs.com/mustone/p/5262848.html
Copyright © 2011-2022 走看看