zoukankan      html  css  js  c++  java
  • Executable WARs with Jetty « EclipseSource Blog

    Executable WARs with Jetty « EclipseSource Blog

    on Oct 2nd, 2009Executable WARs with Jetty

    Today I want to talk about one of the younger members in the Eclipse family: Jetty. It is great to have such an interesting project on board and it is yet another example of how Eclipse has become more than just an IDE.

    What I wanted to with jetty was to create an executable, standalone and self-contained WAR. I first encountered this concept in Hudson. The hudson.war contains an embedded Winstone servlet container, which makes it possible to run the application by executing

    java -jar hudson.war

    This makes test driving the application really simple. The idea was to do the same with Jetty. Embedding the Jetty runtime in the war proved to be the easy part, as it was just a matter of declaring the jetty dependencies in the maven pom.xml.

    The tricky part was telling jetty where to find the war-file to serve. My first try was to hardcode the filename, but that left a foul aftertaste. Finding a solution took quite some time, which is why I am posting this for future reference. This is the Main-Class used to bootstrap Jetty (adapted from the Wicket quickstart archetype):

    import org.mortbay.jetty.Connector;
    import org.mortbay.jetty.Server;
    import org.mortbay.jetty.bio.SocketConnector;
    import org.mortbay.jetty.webapp.WebAppContext;
     
    public class Start {
     
      public static void main(String[] args) throws Exception {
        Server server = new Server();
        SocketConnector connector = new SocketConnector();
     
        // Set some timeout options to make debugging easier.
        connector.setMaxIdleTime(1000 * 60 * 60);
        connector.setSoLingerTime(-1);
        connector.setPort(8080);
        server.setConnectors(new Connector[] { connector });
     
        WebAppContext context = new WebAppContext();
        context.setServer(server);
        context.setContextPath("/");
     
        ProtectionDomain protectionDomain = Start.class.getProtectionDomain();
        URL location = protectionDomain.getCodeSource().getLocation();
        context.setWar(location.toExternalForm());
     
        server.addHandler(context);
        try {
          server.start();
          System.in.read();
          server.stop();
          server.join();
        } catch (Exception e) {
          e.printStackTrace();
          System.exit(100);
        }
      }
    }

    The interesting bit is the getProtectionDomain()/getCodeSource() part, which tells us the location of the war-file. That’s all there is to it. Presto, executable web-application powered by Jetty in jar.

    Edit: Added the import statements as per Tim’s suggestion.

  • 相关阅读:
    ThickBox弹出框的使用方法
    DATASET排序
    jQuery重要插件!
    获取所有querystring变量名
    using要写多少
    【MM系列】SAP MM模块-关于批次特性的查看和获取
    【MM系列】SAP SAP的账期分析和操作
    【ABAP系列】SAP ABAP基础-abap数据类型的解析整理
    【ABAP系列】SAP ABAP基础-录制BDC的MODE定义解析
    【ABAP系列】SAP ABAP基础-数据更新至数据库操作解析
  • 原文地址:https://www.cnblogs.com/lexus/p/2386122.html
Copyright © 2011-2022 走看看