简体   繁体   English

修改过滤器链-或选择servlet以使用过滤器响应请求

[英]Modify the filter chain - Or select servlet to respond to request using filter

I am trying to use a filter to map requests. 我正在尝试使用过滤器来映射请求。 I am trying to do this for two reasons, firstly to dynamically generate URI's and have them mapped to the appropriate servlet and secondly to catch URI's which are not registered and handle them appropriately. 我尝试这样做有两个原因,首先是动态生成URI并将其映射到适当的servlet,其次要捕获未注册的URI并适当地处理它们。

So I'm using a catch-all filter to process the URI and determine the response. 因此,我使用了一个包罗万象的过滤器来处理URI并确定响应。 I would like some way of modifying the filter chain, or some way to set the servlet which responds to the request from within the filter. 我想要某种修改过滤器链的方法,或者某种方式来设置对来自过滤器内部请求的servlet。 I have been unsuccessful using filterConfig.getServletContext().getRequestDispatcher().forward() to send to jsp, ideally though I would like to map to a servlet but can't figure out how. 我一直没有成功使用filterConfig.getServletContext().getRequestDispatcher().forward()发送到jsp,理想情况下,尽管我想映射到servlet但无法弄清楚如何。

The reason I am not doing this from within a servlet is that I have some URIs which are fixed within web.xml and if I use a catch-all servlet those URIs do not get mapped. 我之所以没有从servlet内执行此操作,是因为我有一些在web.xml中固定的URI,并且如果我使用了一个包罗万象的servlet,那么这些URI不会被映射。 Is this possible, is it clean or it going to get really messy? 这可能是干净的还是变得很凌乱?

I don't think this is the right way to do it. 我不认为这是正确的做法。

If you look at what web MVC frameworks do, they have a front controller servlet that maps URLs to controllers, which themselves can accept HTTP requests and return HTTP responses. 如果您看一下Web MVC框架的功能,它们有一个前端控制器servlet,该servlet将URL映射到控制器,控制器本身可以接受HTTP请求并返回HTTP响应。 I think that's a design worth emulating, not your filter idea. 我认为这是值得模仿的设计,而不是您的过滤器创意。

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

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