Dynamically add a servlet to the servletConfig

后端 未结 3 926
不知归路
不知归路 2020-12-16 20:13

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 se

3条回答
  •  无人及你
    2020-12-16 20:33

    There is no standard Servlet API to accomplish this.

    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);
    

    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.

提交回复
热议问题