码头:动态删除注册的servlet [英] Jetty : Dynamically removing the registered servlet
本文介绍了码头:动态删除注册的servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用WebAppContext创建并启动了码头服务器.我还可以使用addServlet方法将servlet添加到WebAppContext中.但我想动态删除此servlet. 我怎样才能做到这一点 ? WebAppContext中未提供诸如removeServlet()之类的东西.
I created and started jetty server with WebAppContext. I can also add servlet to the WebAppContext with addServlet method. But I want to dynamically remove this servlet. How can I do this ? Something like removeServlet() is not provided in the WebAppContext.
推荐答案
您需要手动进行操作(可能应该有一种便捷方法,但没有)
You need to do it manually (there probably should be a convenience method, but there isn't)
在Jetty 7中,它类似于( unested ):
In Jetty 7 it would be something like (untested):
public void removeServlets(WebAppContext webAppContext, Class<?> servlet)
{
ServletHandler handler = webAppContext.getServletHandler();
/* A list of all the servlets that don't implement the class 'servlet',
(i.e. They should be kept in the context */
List<ServletHolder> servlets = new ArrayList<ServletHolder>();
/* The names all the servlets that we remove so we can drop the mappings too */
Set<String> names = new HashSet<String>();
for( ServletHolder holder : handler.getServlets() )
{
/* If it is the class we want to remove, then just keep track of its name */
if(servlet.isInstance(holder.getServlet()))
{
names.add(holder.getName());
}
else /* We keep it */
{
servlets.add(holder);
}
}
List<ServletMapping> mappings = new ArrayList<ServletMapping>();
for( ServletMapping mapping : handler.getServletMappings() )
{
/* Only keep the mappings that didn't point to one of the servlets we removed */
if(!names.contains(mapping.getServletName()))
{
mappings.add(mapping);
}
}
/* Set the new configuration for the mappings and the servlets */
handler.setServletMappings( mappings.toArray(new ServletMapping[0]) );
handler.setServlets( servlets.toArray(new ServletHolder[0]) );
}
这篇关于码头:动态删除注册的servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文