动态添加一个servlet到servletConfig [英] Dynamically add a servlet to the servletConfig
问题描述
我有一个使用插件架构的 Java Web 应用程序.我想知道是否有人有一个解决方案,可以在 Web 应用程序运行时添加一个 servlet,并将 serlvet 映射到 servletconfig?这个想法是可以将一个类添加到/WEB-INF/classes 文件夹中,并在不重新启动 Web 应用程序的情况下使其作为 servlet 处于活动状态.同样,如果用户选择删除插件",则让代码从 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.
推荐答案
没有标准的 Servlet API 来实现这一点.
There is no standard Servlet API to accomplish this.
您可以在 Tomcat 中执行此操作.在您的 web 应用程序中,您的主 servlet(创建其他 servlet)必须实现 ContainerServlet
,以便您可以获取 Wrapper
对象.安装好类文件后,您可以进行以下调用,
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屋!