Servlet3.0新特性WebFilter(Annotation Filter)详解
摘要:
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() { } } |