servlet中filter和chain有什么用? [英] What is the use of filter and chain in servlet?

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

问题描述

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 过滤器是 责任链模式

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
}

您还可以选择调用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中filter和chain有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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