我需要以编程方式在Jetty6服务器上添加和删除servlet。虽然添加它几乎很简单,但我找不到有效的方法来删除它。就我的目的而言,添加和删除servlet非常重要,因为它与动态组件体系结构相关联。当我添加一个组件时,我需要添加一个新的服务,当我移除组件时,我需要删除该服务。
为了添加servlet,我使用了以下模式:
Server server = new Server(8080);
class MyServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.getOutputStream().write("Hello World!".getBytes());
}
}
...
public void addServlet(HttpServlet s, String path)
{
Context root = new Context(server,"/",Context.SESSIONS);
root.addServlet(new ServletHolder(new MyServlet()), "/test/*");
root.getServletHandler().
}
public void removeServlet(HttpServlet s, String path)
{
//What I have to put here ? There is no removeServlet like methods in server/Context/ServletHolder
}为什么删除servlet不是那么明显?你能给我解释一下动机吗?
发布于 2012-03-10 04:21:23
首先,我建议更新到jetty 7或8,如果可能的话,jetty 6在这一点上是相当老的,并且缺乏过去几年在7和8中存在的开发。见鬼,jetty 9现在正在积极地工作。
其次,我不会在servlet级别上考虑这一点,而是在处理程序级别上,与服务器一起添加和删除处理程序,这些处理程序可以是静态资源类型处理程序,也可以是完全成熟的servlet上下文处理程序,甚至是webapp上下文处理程序。
至于为什么servlet上下文处理程序没有删除servlet类型的操作,在该级别删除活动servlet实际上不是servlet规范的一部分,更适合于war部署/取消部署级别。尽管我已经尝试过在servlet上下文处理程序级别添加和删除它们,但这似乎有问题,然后添加更多内容,所以我怀疑删除上下文本身并添加一个新上下文将是此时您最好的选择。
发布于 2012-03-10 18:54:48
发布于 2012-03-20 21:17:58
这个解决方案似乎起作用了:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.ServletMapping;
public class MyServer extends Server
{
ServletHandler sh = new ServletHandler();
public MyServer()
{
super(9090);
setHandler(sh);
test();
}
class MyServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.getWriter().println("CIAO!");
}
}
void test()
{
MyServlet ms = new MyServlet();
addServlet(ms, "/ciao/*");
//removeServlet(ms);//uncomment this ilne in order to remove the servlet right after the deploy
}
public void addServlet(HttpServlet s, String path)
{
sh.addServletWithMapping(new ServletHolder(s), path);
for (ServletHolder so : sh.getServlets())
try
{
System.out.println((so.getServlet() == s));
} catch (ServletException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void removeServlet(HttpServlet s)
{
try
{
HashSet<String> names = new HashSet<String>();
for (ServletHolder so : sh.getServlets())
if (so.getServlet() == s)
names.add(so.getName());
HashSet<ServletMapping> sms = new HashSet<ServletMapping>();
for (ServletMapping sm : sh.getServletMappings())
{
if (!names.contains(sm.getServletName()))
sms.add(sm);
}
sh.setServletMappings(sms.toArray(new ServletMapping[] {}));
} catch (ServletException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}https://stackoverflow.com/questions/9637975
复制相似问题