不管是JSP还是Servlet,其Filter过滤器都是一个东西。因为JSP的本质就是Servlet,只不过是被包装了一下,成了标准,让Web服务器都遵循这个标准来解析编译执行而已。
以下演示的就是在JSP中如何过滤脏话(Servlet的处理方式也差不多)。
package com.filter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class WordsFilter implements Filter {
private Map< String, String>map = new HashMap< String, String>();
// 过滤器的初始化
public void init(FilterConfig config) throws ServletException {
String filePath = config.getInitParameter("filePath");
// 从配置文件中取得文件的相对路径
ServletContext context = config.getServletContext();
String realPath = context.getRealPath(filePath);
// 根据相对路径取得绝对路径
try {
FileReader freader = new FileReader(realPath);
// 根据绝对路径,通过文件流来读取文件
BufferedReader br = new BufferedReader(freader);
String line = null;
while((line=br.readLine()) != null) {
String []str = line.split("=");
map.put(str[0], str[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 乱码处理
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
HttpServletRequest HttpReq = (HttpServletRequest) request;
HttpReqWrapper hrw = new HttpReqWrapper(HttpReq);
hrw.setMap(map);
chain.doFilter(hrw, response);
}
public void destroy() {
System.out.println("--过滤器的销毁--");
}
}
package com.filter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class HttpReqWrapper extends HttpServletRequestWrapper {
private Map< String,String> map = null;
public HttpReqWrapper(HttpServletRequest request) {
super(request);
}
// 过滤脏话
public String replace(String str){
StringBuffer sb = new StringBuffer(str);
Setkeys = this.getMap().keySet();
Iteratorit = keys.iterator();
String ss = null;
while(it.hasNext()) {
String key = it.next();
int index = sb.indexOf(key);
if(index != -1) {
if(key != null)
ss = key;
sb.replace(index, index+key.length(), this.getMap().get(key));
}
}
if(ss!=null) {
if (sb.toString().indexOf(ss) == -1) {
return sb.toString();
} else {
return replace(sb.toString());
}
}
return sb.toString();
}
// 重写getParameter()方法
public String getParameter(String str) {
if(str.equals("pager.offset")){
return super.getParameter(str);
}else{
String content = super.getParameter(str);
return replace(content);
}
}
public Map< String,String> getMap() {
return map;
}
public void setMap(Map< String,String> map) {
this.map = map;
}
}
妈的=**
老子=**
狗日的=***
我日=我*
TMD=***
我靠=我*
贱人=**
滚=*
操=*
1. JSP脏话过滤器的实现
2. 不管是JSP还是Servlet,其Filter过滤器都是一个东西。因为JSP的本质就是Servlet,只不过是被包装了一下,成了标准,让Web服务器都遵循这个标准来解析编译执行而已。
3. 以下演示的就是在JSP中如何过滤脏话(Servlet的处理方式也差不多):
4. 一、 过滤器类
5. package com.filter;
6. import java.io.BufferedReader;
7. import java.io.FileNotFoundException;
8. import java.io.FileReader;
9. import java.io.IOException;
10. import java.util.HashMap;
11. import java.util.Iterator;
12. import java.util.Map;
13. import java.util.Set;
14. import javax.servlet.Filter;
15. import javax.servlet.FilterChain;
16. import javax.servlet.FilterConfig;
17. import javax.servlet.ServletContext;
18. import javax.servlet.ServletException;
19. import javax.servlet.ServletRequest;
20. import javax.servlet.ServletResponse;
21. import javax.servlet.http.HttpServletRequest;
22. public class WordsFilter implements Filter {
23.
24. private Map< String, String>map = new HashMap< String, String>();
25. // 过滤器的初始化
26. public void init(FilterConfig config) throws ServletException {
27. String filePath = config.getInitParameter("filePath");
28. // 从配置文件中取得文件的相对路径
29. ServletContext context = config.getServletContext();
30. String realPath = context.getRealPath(filePath);
31. // 根据相对路径取得绝对路径
32. try {
33. FileReader freader = new FileReader(realPath);
34. // 根据绝对路径,通过文件流来读取文件
35. BufferedReader br = new BufferedReader(freader);
36. String line = null;
37. while((line=br.readLine()) != null) {
38. String []str = line.split("=");
39. map.put(str[0], str[1]);
40. }
41. } catch (FileNotFoundException e) {
42. e.printStackTrace();
43. } catch (IOException e) {
44. e.printStackTrace();
45. }
46.
47.
48. }
49. public void doFilter(ServletRequest request, ServletResponse response,
50. FilterChain chain) throws IOException, ServletException {
51. // 乱码处理
52. request.setCharacterEncoding("gb2312");
53. response.setContentType("text/html;charset=gb2312");
54. HttpServletRequest HttpReq = (HttpServletRequest) request;
55. HttpReqWrapper hrw = new HttpReqWrapper(HttpReq);
56. hrw.setMap(map);
57. chain.doFilter(hrw, response);
58. }
59. public void destroy() {
60. System.out.println("--过滤器的销毁--");
61. }
62.
63. }
64. 二、请求包装器
65. package com.filter;
66. import java.util.Iterator;
67. import java.util.Map;
68. import java.util.Set;
69. import javax.servlet.http.HttpServletRequest;
70. import javax.servlet.http.HttpServletRequestWrapper;
71. public class HttpReqWrapper extends HttpServletRequestWrapper {
72. private Map< String,String> map = null;
73. public HttpReqWrapper(HttpServletRequest request) {
74. super(request);
75. }
76. // 过滤脏话
77. public String replace(String str){
78. StringBuffer sb = new StringBuffer(str);
79. Setkeys = this.getMap().keySet();
80. Iteratorit = keys.iterator();
81. String ss = null;
82. while(it.hasNext()) {
83. String key = it.next();
84. int index = sb.indexOf(key);
85. if(index != -1) {
86. if(key != null)
87. ss = key;
88. sb.replace(index, index+key.length(), this.getMap().get(key));
89. }
90. }
91. if(ss!=null) {
92. if (sb.toString().indexOf(ss) == -1) {
93. return sb.toString();
94. } else {
95. return replace(sb.toString());
96. }
97. }
98. return sb.toString();
99. }
100. // 重写getParameter()方法
101. public String getParameter(String str) {
102. if(str.equals("pager.offset")){
103. return super.getParameter(str);
104. }else{
105. String content = super.getParameter(str);
106. return replace(content);
107. }
108. }
109. public Map< String,String> getMap() {
110. return map;
111. }
112. public void setMap(Map< String,String> map) {
113. this.map = map;
114. }
115.
116.
117. }
118. 三、脏话列表 Profanity.txt
119. 妈的=**
120. 老子=**
121. 狗日的=***
122. 我日=我*
123. TMD=***
124. 我靠=我*
125. 贱人=**
126. 滚=*
127. 操=*
128. 四、web.xml中配置过滤器
129. <filter>
130. <filter-name>WordsFilterfilter-name>
131. <filter-class>com.filter.WordsFilterfilter-class>
132. <init-param>
133. <param-name>filePathparam-name>
134. <param-value>/WEB-INF/Profanity.txtparam-value>
135. init-param>
136. filter>
137. <filter-mapping>
138. <filter-name>WordsFilterfilter-name>
139. <url-pattern>/getNote.jspurl-pattern>
140. filter-mapping>
1、
2、
By CodingMouse
2010年4月9日