Servlet-Filter

本文详细介绍了Servlet过滤器的概念及其工作原理。过滤器可以对请求进行预处理或对响应进行后处理,实现如身份验证、日志记录等多种功能。文章还列举了多种过滤器的应用实例,并解释了其初始化、处理请求及销毁的过程。

javax.servlet 
Interface Filter


public interface Filter

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. 

Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.

Filters are configured in the deployment descriptor of a web application

Examples that have been identified for this design are
1) Authentication Filters 
2) Logging and Auditing Filters 
3) Image conversion Filters 
4) Data compression Filters 
5) Encryption Filters 
6) Tokenizing Filters 
7) Filters that trigger resource access events 
8) XSL/T filters 
9) Mime-type chain Filter 

Since:
Servlet 2.3

Method Summary
 voiddestroy() 
          Called by the web container to indicate to a filter that it is being taken out of service.
 voiddoFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
          The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
 voidinit(FilterConfig filterConfig) 
          Called by the web container to indicate to a filter that it is being placed into service.
 

Method Detail

init

void init(FilterConfig filterConfig)
          throws ServletException
Called by the web container to indicate to a filter that it is being placed into service. The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work. 

The web container cannot place the filter into service if the init method either
1.Throws a ServletException 
2.Does not return within a time period defined by the web container

Throws:
ServletException

doFilter

void doFilter(ServletRequest request,
              ServletResponse response,
              FilterChain chain)
              throws java.io.IOException,
                     ServletException
The  doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

A typical implementation of this method would follow the following pattern:- 
1. Examine the request
2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering 
3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering 
4. a) Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()), 
4. b) or not pass on the request/response pair to the next entity in the filter chain to block the request processing
5. Directly set headers on the response after invocation of the next entity in the filter chain.

Throws:
java.io.IOException
ServletException

destroy

void destroy()
Called by the web container to indicate to a filter that it is being taken out of service. This method is only called once all threads within the filter's doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call the doFilter method again on this instance of the filter. 

This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter's current state in memory.

在包含 `AutoLoginFilter`、`LoginServlet`、`LogoutServlet` 的 `web.xml` 配置代码中,`servlet` 报错可能由多种原因导致,以下是一些常见问题及对应的解决办法: ### 1. 类路径问题 若 `servlet` 类的全限定名配置有误,或者类文件未被正确部署到 `WEB-INF/classes` 目录下,就会引发报错。 ```xml <servlet> <servlet-name>LoginServlet</servlet-name> <!-- 确保此处类路径正确 --> <servlet-class>com.example.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>LogoutServlet</servlet-name> <!-- 确保此处类路径正确 --> <servlet-class>com.example.LogoutServlet</servlet-class> </servlet> ``` 解决办法:检查 `servlet` 类的全限定名是否正确,并且确认类文件已被正确部署到 `WEB-INF/classes` 目录下。 ### 2. 缺少必要的依赖 如果 `servlet` 类依赖了某些库,但这些库未被正确添加到项目的类路径中,也会导致报错。 解决办法:检查项目的依赖配置,确保所有必要的库都已添加到 `WEB-INF/lib` 目录下。 ### 3. 配置重复 若 `web.xml` 中存在重复的 `servlet` 名称或者映射,会造成冲突并引发报错。 ```xml <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.example.LoginServlet</servlet-class> </servlet> <!-- 不能有重复的 servlet-name --> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.example.AnotherLoginServlet</servlet-class> </servlet> ``` 解决办法:检查 `web.xml` 文件,确保每个 `servlet` 名称和映射都是唯一的。 ### 4. 过滤器配置问题 对于 `AutoLoginFilter`,若其配置不正确,也可能影响 `servlet` 的正常运行。 ```xml <filter> <filter-name>AutoLoginFilter</filter-name> <filter-class>com.example.AutoLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>AutoLoginFilter</filter-name> <!-- 确保映射路径正确 --> <url-pattern>/*</url-pattern> </filter-mapping> ``` 解决办法:检查过滤器的配置,确保 `filter-class` 路径正确,并且 `filter-mapping` 的 `url-pattern` 符合需求。 ### 5. 语法错误 `web.xml` 文件存在语法错误,如标签未正确闭合、属性值错误等,会导致解析失败。 解决办法:使用 XML 验证工具或者 IDE 的 XML 验证功能,检查并修复 `web.xml` 文件中的语法错误。 ### 6. 版本兼容性问题 若 `web.xml` 的版本与应用服务器不兼容,也可能引发报错。 ```xml <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置内容 --> </web-app> ``` 解决办法:检查 `web.xml` 的版本声明,确保与应用服务器的版本兼容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值