servlet中过滤器和链的用途是什么? [英] What is the use of filter and chain in servlet?

查看:248
本文介绍了servlet中过滤器和链的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

chain.doFilter(req,res);

我们在servlet程序中使用它。我想知道servlet中方法 doFilter()的用法是什么?
Java servlet中过滤器和链概念的用途是什么?

chain.doFilter(req,res);
We used this in a servlet program. I want to know what is the use of the method doFilter() in a servlet? Also what is the use of filter and chain concept in Java servlets?

推荐答案

Servlet过滤器是< a href =http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern =noreferrer>责任链模式

Servlet filters are implementation of the chain of responsibility pattern

关键是每个过滤器都保持在它所映射到的每个servlet的前面和后面。因此,如果你有一个servlet周围的过滤器,你将拥有:

The point is that each filter stays "in front" and "behind" each servlet it is mapped to. So if you have a filter around a servlet, you'll have:

void doFilter(..) { 
    // do stuff before servlet gets called

    // invoke the servlet, or any other filters mapped to the target servlet
    chain.doFilter(..);

    // do stuff after the servlet finishes
}

您还可以选择 not 来调用 chain.doFilter(..),在这种情况下,永远不会调用servlet。这对于安全目的很有用 - 例如,您可以检查是否有用户登录。

You also have the option not to call chain.doFilter(..) in which case the servlet will never be called. This is useful for security purposes - for example you can check whether there's a user logged-in.

这篇关于servlet中过滤器和链的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆