如何动态地将servlet添加到jetty服务器?

时间:2014-09-12 20:15:22

标签: servlets jetty embedded-jetty jetty-8

我创建了一个jetty服务器,并希望以编程方式动态添加servlet。

我设置了日志记录,然后控制台继续创建服务器,如下所示:

Connector               httpConn        = null;
Connector               httpsConn       = null;
HandlerCollection       handlers        = new HandlerCollection();
httpConn = new SelectChannelConnector();
httpConn.setPort( httpPort );
httpConn.setName( "HTTPConnector" );  // simillar setup for httpsConn if needed

contextHandlers = new ContextHandlerCollection();
if( launchServlets ) {
    // this is more or less the same servlet creation code as below but for launching
    // static servlets (in this example, there are none)
    holders = initializeServlets( configFilePath );
}
handlers.addHandler( contextHandlers );

server = new Server();
if( httpConn != null ) {
    server.addConnector( httpConn );
}
if( httpsConn != null ) {
    server.addConnector( httpsConn );
}

server.setGracefulShutdown( 1000 ); /
server.setStopAtShutdown( true );
server.setHandler( handlers );
server.start();

if( launchServlets ) {
    // Catch any exception that occurs during servlet init()
    // (must be called after server.start() )
    for( int i = 0; i < holders.length; i++ ) {
        if( holders[ i ] != null ) {
            Exception initEx = holders[ i ].getUnavailableException();
            if( initEx != null ) {
                server.stop();
                throw initEx;
            }
        }
    }
}

因此,当我需要启动servlet时,我会执行以下操作:

boolean isSecureOnly = false;  // function that decides suppressed for now
String[] connectors;

MyServlet myServlet = new MyServlet();

ServletHolder holder = new ServletHolder( myServlet );
holder.setInitParameter( Constants.CONF_INIT_STRING_PARAM, configString );
holder.setInitParameter( Constants.CONF_INIT_NAME_PARAM, myName );
ServletContextHandler handler = new ServletContextHandler();
if( details != null ) {
    details.sch = handler;
}
String contextPath = MyConfig.getContextPath( myProps, myName );
handler.setContextPath( contextPath );
handler.setAllowNullPathInfo( true );

// bind the servlet to the connectors it should be listening to
if( isSsl ) {
    if( isSecureOnly ) {
        connectors = new String[ 1 ];
        connectors[0] = Constants.CONF_HTTPS_CONNECTOR;
    } else {
        connectors = new String[ 2 ];
        connectors[0] = Constants.CONF_HTTPS_CONNECTOR;
        connectors[1] = Constants.CONF_HTTP_CONNECTOR;
    }
} else {
    if( isSecureOnly ) {
    throw new ConfigException( 50051 );
    }
    connectors = new String[ 1 ];
    connectors[0] = Constants.CONF_HTTP_CONNECTOR;
}
handler.setConnectorNames( connectors );

if( myName != null ) {
    handler.setDisplayName( MyMessage.message( 10025, myName ) );
} else {
    handler.setDisplayName( MyMessage.message( 10001 ) );
}
handler.addServlet( holder, "/*" );
contextHandlers.addHandler( handler );

contextHandlers.mapContexts();
Exception initEx = holder.getUnavailableException();
if( initEx != null ) {
    // deal with error
}

虽然我的动态servlet启动代码似乎没有错误,但控制台没有显示servlet初始化。当我有launchServlets = true时,代码将静态启动servlet,一切都很好,但我想动态启动它们。

我是否必须手动使servlet初始化(我认为servlet被隔离到自己的内存空间中)?如何告诉Jetty启动新的Servlet?

更新:到目前为止,我设法让servlet启动的唯一方法是server.stop()/ server.join()/ server.start()。我想添加servlet(并删除 - 但这是一个额外的问题)不要中断对现有servlet的服务)?

我正在使用jetty-all-server-8.1.3和servlet api 3.0

提前感谢您的帮助/提示!

1 个答案:

答案 0 :(得分:1)

我不确定这是否是安全的方法,但我已经通过在ServletContextHandler()上调用start()而不是停止和启动服务器来启动serlvlet。

这适用于8.1.3和9.2.4(我升级到升级到servlet api的3.1)

希望这有助于其他有兴趣做类似事情的人。