One of my projects uses Spring MVC to handle URL mappings and dispatch logic. I have now to use a third party library which uses its own HttpServlet as the main entry point for its functionalities, but as it’s an optional drop-in replacement for another library I can’t just put the <servlet> declaration in the web.xml: I’d rather use a Controller and Spring profiles to switch between such implementations without having to edit the web.xml.
Is there anything offered OOTB by Spring to handle such cases? I don’t seem to find it right away.
Thanks in advance!
Answering my own question here just in case my approach can be useful to others.
There are two key factors I needed to consider:
In my specific case there’s no need for properly handling the servlet destruction, as it’s a no-op.
I ended up writing a dedicated
Controller, to be instantiated only if a specific Spring profile is activated, which takes care of instantiating and initializing the Servlet. Then, all the requests will be directly handled in avoidhandler method, as follows:The
delegating-servlet.xmlfor theDispatcherServletlooks like the following: