【问题标题】:How to create a servlet on the fly如何动态创建 servlet
【发布时间】:2013-05-13 19:18:51
【问题描述】:

对于以下 URL,我收到 404 错误,这很好,因为 URL 不存在。是否可以在返回错误页面之前在服务器运行时创建这些 servlet?

http://127.0.0.1:8888/test1
http://127.0.0.1:8888/test1/test2

我正在考虑创建一个通用控制器,如果当前 servlet 不存在,则拦截所有 url,然后创建它?

【问题讨论】:

    标签: spring google-app-engine servlets


    【解决方案1】:

    假设Spring 你的意思是Spring MVC,你可以这样做:

    @Controller
    public class YourController {
        @RequestMapping("/mappingA")
        public void methodA() {
            // (...)
        }
    
        @RequestMapping("/mappingB")
        public void methodB() {
            // (...)
        }
    
        // Catches all non-matched requests.
        @RequestMapping("/*")
        public void fake404(HttpServletRequest request) {
            var uri = request.getRequestURI();
            // (...)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2020-09-30
      相关资源
      最近更新 更多