简体   繁体   English

过滤器如何处理会话

[英]how Filters handle Session

some of you advise me to handle sessions using filters . 有人建议我使用过滤器 处理会话 I studied a little about the filter following some guides found on the internet, and wrote a filter referring this guide . 我按照在互联网上找到的一些指南对过滤器进行了一些研究,并编写了参考该指南的过滤器。

I saw that the filter is called for every component of my page (css, images etc); 我看到针对页面的每个组件(css,图像等)都调用了过滤器; is there a way to call it just when a jsp or a servlet is load? 是否只有在加载jsp或servlet时才能调用它? I need a method that can understand if jsp or a servlet is load, in order to make some stuff inside my filter. 我需要一种可以了解是否加载了jsp或servlet的方法,以便在过滤器中添加一些内容。

Yes, you can do that. 是的,你可以这么做。 Just change the url-pattern for your session filter. 只需更改会话过滤器的url-pattern

If you are using some web framework (spring mvc,...) with one dispatching servlet, you can map your filter only to this servlet using servlet-name and requests to other resources (js, css) will not be intercepted by this filter. 如果您正在使用带有一个调度Servlet的某个Web框架(spring mvc,...),则可以使用servlet-name将过滤器仅映射到该servlet,并且对其他资源(js,css)的请求将不会被该过滤器拦截。

First off, please don't be misled by the purpose of the tutorial in the link you have specified. 首先,请不要在您指定的链接中被教程的目的所迷惑。 Session handling is always done through cookies, URL-rewriting (or for the more advanced, SSL). 会话处理始终通过cookie,URL重写(或更高级的SSL)来完成。 He's merely using filters to enhance application security , by ensuring the user is redirected to the login page, whenever he goes directly to an "avoid-url". 他只是通过确保用户每次直接转到“避免网址”时都被重定向到登录页面,而仅使用过滤器来增强应用程序安全性

Think about a filter, a physical filter. 考虑一个过滤器,一个物理过滤器。 Whether it be an excel filter or a physical gravel filter. 无论是Excel过滤器还是物理砾石过滤器。 It stands between one thing and another thing: 它介于一件事和另一件事之间:

在此处输入图片说明

Java web filters can do the same thing: Java Web过滤器可以做同样的事情:

在此处输入图片说明

Just like you can choose which water bottle to filter, you can decide which requests you want to filter. 就像您可以选择要过滤的水瓶一样,您可以决定要过滤的请求。 You do that using the filter-mapping element in web.xml . 您可以使用web.xml中filter-mapping元素进行此操作。 You can specify individual servlet names, or a url pattern. 您可以指定单个servlet名称或url模式。

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

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