简体   繁体   English

如何在运行时动态创建,注册和映射servlet

[英]How to create, register and map servlets dynamically at runtime

I have a small spring boot java application project, to implement Proxy approach using Servlets, which contains a maven dependency on a project which provides me a UtilityServlet which I am extending in the TestServlet of my project and registering the servlet mapping using @Bean and @Configuration annotations. 我有一个小型的Spring Boot Java应用程序项目,用于使用Servlet实现代理方法,该项目包含对项目的maven依赖关系,该依赖关系为我提供了UtilityServlet,我将在我的项目的TestServlet中对其进行扩展,并使用@Bean和@注册Servlet映射。配置注释。 My application is working fine with this approach, but i have new use case. 我的应用程序使用这种方法运行良好,但是我有新的用例。

I am trying to be able to modularize/ parameterize the application config. 我试图能够模块化/参数化应用程序配置。

What I want is to be able to create, register and map servlets at runtime during application startup based on the number of URL patterns that are required to be mapped passed as parameter to application. 我想要的是能够根据应用程序启动时在运行时创建,注册和映射servlet的功能,该操作基于作为参数传递给应用程序的需要映射的URL模式的数量。 For eg 例如

servletUrl.properties contain following 3 url mappings: servletUrl.properties包含以下3个url映射:

servlet.url1=/servlet1/* servlet.url1 = / servlet1 / *
target.url1=/target1/ target.url1 = / target1 /
servlet.url2=/servlet2/* servlet.url2 = / servlet2 / *
target.url2=/target2/ target.url2 = / target2 /
servlet.url3=/servlet3/* servlet.url3 = / servlet3 / *
target.url3=/target3/ target.url3 = / target3 /

On the basis of above mappings I want that on application startup, application recognizes 3 url mappings in the property file and create and register 3 different servlets extending UtilityServlet, one for each mapping. 基于上述映射,我希望在应用程序启动时,应用程序在属性文件中识别3个url映射,并创建并注册3个扩展UtilityServlet的不同servlet,每个servlet对应一个。

Is there a way to accomplish something like this, Spring solution would be preferred but not necessarily a deal breaker. 有没有办法完成这样的事情,Spring解决方案将是首选,但不一定会破坏交易。 Thanks in advance guys 在此先感谢大家

Let your single Servlet implementation call a routing class. 让您的单个Servlet实现调用路由类。

The Router reads the config file and sets up different handler classes depending on its content. 路由器读取配置文件,并根据其内容设置不同的处理程序类。

When calls come from the Servlet, they get routed on calling-url to the correct handler-class. 当调用来自Servlet时,它们将在calling-url上路由到正确的处理程序类。

For instance, your base servlet can have a base url mapping of 'api'. 例如,您的基本servlet可以具有“ api”的基本url映射。 When calls come in with say 'api/target1?param=etc' you just route on 'target1' to the correct handler. 当呼叫说出“ api / target1?param = etc”时,您只需将“ target1”路由到正确的处理程序即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM