动态地将servlet添加到servletConfig [英] Dynamically add a servlet to the servletConfig

查看:223
本文介绍了动态地将servlet添加到servletConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用插件架构的Java Web应用程序。我想知道如果有人有一个解决方案,通过一个可以添加一个servlet,serlvet映射到servletconfig当web应用程序运行时?这个想法是,一个类可以被添加到/ WEB-INF / classes文件夹,并作为servlet被激活,而不需要重新启动web应用程序。同样的性质,如果用户选择删除插件,然后让代码从servletconfig中删除该类。

I have a Java web application that uses a plugin architecture. I would like to know if anyone has a solution where by one could add a servlet, with serlvet mapping to the servletconfig while the web app is running? The idea being that a class could be added to the /WEB-INF/classes folder and be made active as a servlet without restarting the web app. By the same nature, if the user chooses to remove the "plugin" then have the code remove the class from the the servletconfig.

推荐答案

p>没有标准的Servlet API来实现这一点。

There is no standard Servlet API to accomplish this.

你可以在Tomcat中做到这一点。在你的webapp,你的主servlet(一个创建其他人)必须实现 ContainerServlet ,所以你可以抓住包装目的。一旦你安装了你的类文件,你可以进行以下调用

You can do this in Tomcat. In your webapp, your master servlet (the one creates others) must implements ContainerServlet so you can get hold of the Wrapper object. Once you have your class file installed, you can make following calls,

Context context = (Context) wrapper.getParent();
Wrapper newWrapper = context.createWrapper();
newWrapper.setName(name);
newWrapper.setLoadOnStartup(1);
newWrapper.setServletClass(servletClass);
context.addChild(newWrapper);
context.addServletMapping(pattern, name);

这些调用在运行时创建一个servlet。你需要找到坚持这些信息的方法。您可以通过更新web.xml或写入您自己的文件来完成此操作。

These calls create a servlet on the fly. You need to find way to persist this information. You can do this by updating web.xml or write to your own file.

这篇关于动态地将servlet添加到servletConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆