How to install a servlet in a tomcat container and load it into each context of a web application?

I am currently using Tomcat 7. I want to deploy / install a servlet that will be loaded into every webapp in their context. I am looking for a solution that does not include adding a servlet to every webapp war. Is it possible?

Ultimately, I want it to serve requests on the common subpath of each webapp context root.

I thought maybe I could load the annotated servlet from a regular tomcat class loader, but I couldn't get this to work. For example, the same annotated servlet worked in war, but not in a regular classloader.

@WebServlet( description = "Says Hello", urlPatterns = { "/HelloServlet" }) public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // log hello } } 

Thanks for the help.

+5
source share
2 answers

What you are describing is not entirely compatible with Servlets logic.

If you want to use a service / library from two different applications in Tomcat, you can add the library as a shared library in Tomcat. You can then use this library from your application servlets. If you want to achieve this, you need to add the jar to $CATALINA_HOME/shared/lib , and then edit $CATALINA_HOME/conf/catalina.properties to add the {catalina.home}/mylibs/*.jar common.loader to common.loader . You can then use your library in various servlets of your applications.

However, this is different from what you are describing. In fact, what you are describing is not possible in Tomcat, since each web application has its own ApplicationContext. So the webappA application context will be http: // host / webappA and the webappB application context will be http: // host / webappB . So, if you want to have a servlet outside of these two applications, this servlet will belong to another ApplicationContext. Thus, it will not be possible to access this servlet through any of the paths http: // host / webappA / common , http: // host / webappB / common , which are related to previous ApplicationContexts.

0
source

You can do this using a war overlay .

0
source

All Articles