本文实例为大家分享了servlet过滤器的具体代码,供大家参考,具体内容如下
1.servlet过滤器产生背景
项目中我们会遇到这样一类的需求,对访问服务器的用户ip实施过滤,只有在允许名单中的ip才能访问服务,为了实现需求,每当有客户端请求时,我们都会写校验ip的代码,客户端能够访问到的servlet我们都需要这样做,很明显这样做有一个缺点,就是代码冗余,维护不方便,如果验证规则改变,修改起来也特别麻烦。
为了解决以上问题,Filter 技术应运而生。
2.servlet过滤器是什么?
servlet过滤器是在java servlet 2.3 中定义的。它能够对servlet容器传给servlet组件的ServletRequest 对象和 ServletResponse对象进行检查和修改。
3.案例
所有servlet过滤器都要实现javax.servlet.Filter接口。
NoteFilter
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package com.learn; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/29. */ public class NoteFilter implements Filter { private FilterConfig config = null ; private String ipTable = null ; //ip名单 @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println( "note filter initial" ); this .config = filterConfig; this .ipTable = config.getInitParameter( "ipTable" ); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println( "do filter starting" ); //校验ip地址 if (!veryfyIP(request,response)) return ; long befor = System.currentTimeMillis(); config.getServletContext().log( "before call note Filter" ); chain.doFilter(request,response); config.getServletContext().log( "after call note Filter" ); long after = System.currentTimeMillis(); String name = "" ; if (request instanceof HttpServletRequest){ name = ((HttpServletRequest) request).getRequestURI(); } config.getServletContext().log( "Note Filter : name:" +name + " time :" +(after -befor)+ "ms" ); } @Override public void destroy() { } private boolean veryfyIP(ServletRequest request, ServletResponse response){ String ip = request.getRemoteAddr(); System.out.println( "请求ip:" +ip); System.out.println( "ipTable 黑名单:" +ipTable); if (ip.indexOf(ipTable) == - 1 ){ System.out.println( "校验不通过" ); response.setContentType( "text/html" ); PrintWriter out = null ; try { out = response.getWriter(); out.print( "<h1>对不起,你的ip不能访问服务器</h1>" ); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return false ; } else { return true ; } } } |
NoteServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.learn; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by Administrator on 2017/09/29. */ public class NoteServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println( "已经通过了过滤器" ); } } |
web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
|
< filter > < filter-name >ip</ filter-name > < filter-class >com.learn.NoteFilter</ filter-class > < init-param > < param-name >ipTable</ param-name > < param-value >127.0.0.1</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >ip</ filter-name > < url-pattern >/note</ url-pattern > </ filter-mapping > |
1
2
3
4
5
6
7
8
|
< servlet > < servlet-name >note</ servlet-name > < servlet-class >com.learn.NoteServlet</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >note</ servlet-name > < url-pattern >/note</ url-pattern > </ servlet-mapping > |
结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/wwyx-xi/p/7613011.html