0

Is it possible to replace a ServletRegistration via servlet-api?

Following code can only register new Servlets for the given servlet name, but if a servlet with the name exist, it does nothing.

ServletContext.addServlet( "servletName", ServletClass.class );

So I'm searching for a possibility to replace the registration (other ServletClass by same name) or to remove one by name.

chuem
  • 111
  • 1
  • 9

1 Answers1

0

The facility to add servlets is provided during servlet context initialization. Given that it is impossible to add new servlets at runtime i.e. anytime after the Servlet Context is initialised, you should add the correct set of servlets during the servlet context initialization.

There is really no point in adding one servlet with a name and then replacing it with another servlet, all inside the servlet context initialization. Isnt it ? Do you see any use-case for it ?

@Balusc actually suggests a good alternative :

a single controller servlet in combination with command pattern is much better suited. 
You could then add commands (actions) during runtime and intercept on it based on the request URI.
Community
  • 1
  • 1
2020
  • 2,821
  • 2
  • 23
  • 40
  • Our use-case is a changed configuration (servlet behavior) for testing. The Application itself register its servlets with the ServletContextListener and a (test)submodule should overwrite the servlet implementation. (in servlet context initilisation process) But ok, if its impossible we must try other solutions. Thank you – chuem May 29 '13 at 21:57