gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:09:47 25 4
gpt4 key购买 nike

我创建了一个 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

预先感谢您的帮助/提示!

最佳答案

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

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

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

关于servlets - 如何将 servlet 动态添加到 jetty 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25816156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com