Java - dynamically adding a URL pattern to a servlet

Is it possible to add a URL pattern to a Servlet dynamically at runtime? For example, when the servlet starts, scan the annotation folder and then paste these url patterns into the servlet?

  • for clarity -

In Servlet initialization file I want to do this (pseudo code)

// scan all the files in the package my.project.services
// find all the classes with the Annotation @Service
// read those annotations, find the url patterns in them, and insert them into the servlet
+2
source share
1 answer

I'm not sure I understand your ultimate goal, but here is a possible solution.

With Servlet 3.0 we implement the interface ServletContainerInitializer. Register it as javadoc says

JAR , META-INF/services

onStartup(..) -.

. , , , , URL .

, Servlet / ServletContext URL ServletRegistration.Dynamic.

ServletRegistration.Dynamic registration = servletContext.addServlet("myServlet", new MyServlet());
registration.addMapping(yourCollectionAsAStringArray);

, .

+7

Source: https://habr.com/ru/post/1524731/


All Articles