博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet3.0新特性WebFilter(Annotation Filter)详解
阅读量:6257 次
发布时间:2019-06-22

本文共 5854 字,大约阅读时间需要 19 分钟。

摘要:

Servlet3.0作为J2EE 6规范一部分,并随J2EE6一起发布,WeFilter是过滤器注解,是Servlet3.0的新特性,不需要在web.xml进行配置,简化了配置。

Name

Type

Required

Description

filterName

String

Optional

Name of the filter.

value

or

urlPatterns

String[]

Required

Specify one or more URL patterns to which the filter applies. Either of attribute can be used, but not both.

dispatcherTypes

DispatcherType[]

Optional

Specify types of dispatcher to which the filter applies. Default isjavax.servlet.DispatcherType.REQUEST

servletNames

String[]

Optional

Specify names of servlets to which the filter applies.

displayName

String

Optional

Display name of the filter.

description

String

Optional

Description of the filter.

asyncSupported

boolean

Optional

Specify whether the filter supports asynchronous operation mode. Default is false.

initParams

WebInitParam[]

Optional

Specify one or more initialization parameters of the filter. Each parameter is specified by@WebInitParamannotation type.

smallIcon

String

Optional

Specify name of the small icon of the filter.

largeIcon

String

Optional

Specify name of the large icon of the filter.

OneFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package
com.what21.servlet.webfilter;
 
import
java.io.IOException;
 
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.annotation.WebFilter;
 
@WebFilter
(
"/*"
)
public
class
OneFilter
implements
Filter {
 
    
@Override
    
public
void
init(FilterConfig config)
throws
ServletException {
 
    
}
 
    
@Override
    
public
void
doFilter(ServletRequest request, ServletResponse response,
            
FilterChain chain)
throws
IOException, ServletException {
        
System.out.println(
"OneFilter doFilter()"
);
        
chain.doFilter(request, response);
    
}
 
    
@Override
    
public
void
destroy() {
 
    
}
 
}

TwoFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package
com.what21.servlet.webfilter;
 
import
java.io.IOException;
 
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.annotation.WebFilter;
 
@WebFilter
(servletNames =
"MyFourServlet"
)
public
class
TwoFilter
implements
Filter {
 
    
@Override
    
public
void
init(FilterConfig config)
throws
ServletException {
 
    
}
 
    
@Override
    
public
void
doFilter(ServletRequest request, ServletResponse response,
            
FilterChain chain)
throws
IOException, ServletException {
        
System.out.println(
"TwoFilter doFilter()"
);
        
chain.doFilter(request, response);
    
}
 
    
@Override
    
public
void
destroy() {
 
    
}
 
}

ThreeFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package
com.what21.servlet.webfilter;
 
import
java.io.IOException;
 
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.annotation.WebFilter;
 
@WebFilter
(servletNames = {
"MyFourServlet"
,
"MyFiveServlet"
})
public
class
ThreeFilter
implements
Filter {
 
    
@Override
    
public
void
init(FilterConfig config)
throws
ServletException {
 
    
}
 
    
@Override
    
public
void
doFilter(ServletRequest request, ServletResponse response,
            
FilterChain chain)
throws
IOException, ServletException {
        
System.out.println(
"ThreeFilter doFilter()"
);
        
chain.doFilter(request, response);
    
}
 
    
@Override
    
public
void
destroy() {
 
    
}
 
}

FourFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package
com.what21.servlet.webfilter;
 
import
java.io.IOException;
 
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.annotation.WebFilter;
 
@WebFilter
({
"/one"
,
"/two"
})
public
class
FourFilter
implements
Filter {
 
    
@Override
    
public
void
init(FilterConfig config)
throws
ServletException {
 
    
}
 
    
@Override
    
public
void
doFilter(ServletRequest request, ServletResponse response,
            
FilterChain chain)
throws
IOException, ServletException {
        
System.out.println(
"FourFilter doFilter()"
);
        
chain.doFilter(request, response);
    
}
 
    
@Override
    
public
void
destroy() {
 
    
}
 
}

FiveFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package
com.what21.servlet.webfilter;
 
import
java.io.IOException;
 
import
javax.servlet.DispatcherType;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.annotation.WebFilter;
import
javax.servlet.annotation.WebInitParam;
 
@WebFilter
(
    
urlPatterns =
"/five"
,
    
filterName =
"FiveFilter"
,
    
initParams = {
        
@WebInitParam
(name =
"name"
, value =
"username"
),
        
@WebInitParam
(name =
"value"
, value =
"password"
)
    
},
    
description =
"MyFiveFilter"
,
    
dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}
)
public
class
FiveFilter
implements
Filter {
 
    
@Override
    
public
void
init(FilterConfig config)
throws
ServletException {
        
String name = config.getInitParameter(
"name"
);
        
String value = config.getInitParameter(
"value"
);
        
System.out.println(
"name = "
+ name);
        
System.out.println(
"value = "
+ value);
        
System.out.println(
"init()....."
);
    
}
 
    
@Override
    
public
void
doFilter(ServletRequest request, ServletResponse response,
            
FilterChain chain)
throws
IOException, ServletException {
        
System.out.println(
"FiveFilter doFilter()"
);
    
}
 
    
@Override
    
public
void
destroy() {
 
    
}
 
}

  

转载地址:http://isxsa.baihongyu.com/

你可能感兴趣的文章
IO多路复用之select
查看>>
pd_ds中的hash
查看>>
买书不读是一种什么病?
查看>>
微信接口开发报错invalid credential, access_token is invalid or not latest hint
查看>>
nohup 部署springboot 使用命令
查看>>
MQ产品比较-ActiveMQ-RocketMQ
查看>>
暂时没有想好呢。
查看>>
windows服务 MVC之@Html.Raw()用法 文件流的读写 简单工厂和工厂模式对比
查看>>
PHP解析URL并得到URL中的参数
查看>>
【vue.js】绑定click事件
查看>>
字体属性
查看>>
linux的iptables和firewall的区别
查看>>
Install RabbitMQ server in CentOS 7
查看>>
Eureka的优势
查看>>
Android项目实战(一): SpannableString与SpannableStringBuilder
查看>>
idea中的language level 介绍
查看>>
CTSC 2018酱油记
查看>>
php框架排名(Laravel一直第一)
查看>>
行贿罪、受贿罪刑事辩护6大要点
查看>>
java日期格式(年月日时分秒毫秒)
查看>>