Servlet- Filter-实例-非法字符过滤器
这篇文章主要讲怎么写一个servlet过滤器,自己做的一个小实验
1.Servlet过滤器
过滤器的使用主要是通过三个核心接口,Filter接口、FilterChain接口、FilterConfig接口;Filter接口位于javax.servlet包中,当定义一个过滤器对象时需要实现此接口,重写三个函数:
public void init(FilterConfig filterConfig) //过滤器的初始化方法,容器调用此方法完成过滤器的初始化,对于每一个Filter实例此方法只被执行一次
public void doFilter(ServletRequest request,ServletResponse,response,FilterChain filterChain) //与Servlet的service()方法相类似,当请求及响应交给过滤器时,过滤器调用此方法进行过滤处理
public void destroy() //在过滤器生命周期结束时调用次方法,用于释放过滤器所占用的资源
2.非法字符过滤器
在一些大型网站中,需要设有非法字符过滤功能,如色情关键字、脏话等,以免产生不良影响。
(1)创建名为WordFilter的类,实现Filter接口 ,此类不仅可以实现非法字符过滤,还可以对字符编码的转换进行处理,定义两个属性:字符数组words和字符编码encoding,并在init()中对其进行实例化,关键代码:
public class WordFilter implements Filter {
private String[] words;
private String encoding;
/*
* 初始化函数
*/
@Override
public void init(FilterConfig arg0) throws ServletException {
encoding = arg0.getInitParameter("encoding");
words = new String[]{"傻逼","智障","你妹","你麻痹"};
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
//判断字符编码是否有效
if(encoding!=null){
//设置request字符编码
request.setCharacterEncoding(encoding);
//将request转换为重写后的Request对象
request = new Request((HttpServletRequest)request);
//设置response的字符编码
response.setContentType("text/html;charset="+encoding);
}
//传递给下一个拦截器
filterChain.doFilter(request, response);
}
/*
* 销毁函数
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
this.words = null;
this.encoding = null;
}
/*
* 处理过滤字符函数
*/
public String filter(String param){
if(words!=null&&words.length>0){
for(int i=0;i<words.length;i++){
if(param.equals(words[i])){
param="***";
}
}
}
return param;
}
}
(2)获取信息使用ServletRequest对象获取表单所提交的数据(主要是通过getParameter()与getParameterValues()函数,此处只重写前者),在此类中创建内部类Request,关键代码:
此处HttpServletRequestWrapper类是ServletReuquest接口的实现类
class Request extends HttpServletRequestWrapper{
public Request(HttpServletRequest request) {
super(request);
// TODO Auto-generated constructor stub
}
@Override
public String getParameter(String name) {
return filter(super.getRequest().getParameter(name));//返回过滤后的参数值
}
}
(3)编写Servlet类,WordServlet,代码如下:
public class WordServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String testWord = req.getParameter("testS");
System.out.println(testWord);
PrintWriter out = resp.getWriter();
out.println(testWord);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
}
(4)web.xml的配置
<servlet>
<servlet-name>WordServlet</servlet-name>
<servlet-class>com.gyj.servlet.WordServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WordServlet</servlet-name>
<url-pattern>/WordServlet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>WordFilter</filter-name>
<filter-class>com.gyj.filter.WordFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WordFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
(5)jsp页面代码:
此处用到了jqurey,读者可以换成表单提交
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%String path = request.getContextPath();%>
<title>ServletFilter---非法字符过滤器</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
</head>
<script type="text/javascript">
function clickBresult(){
$.post("<%=path%>/WordServlet",{testS:$("#testCharact").val()},function(data){
document.getElementById("result").innerText = data;
});
}
</script>
<body>
<div class = "head">
<input type = "text" name = "testCharact" id = "testCharact" size = "10">
<input type = "button" name = "action" id = "action" value = "测试" onclick = "clickBresult();">
<span name = "result" id = "result" ></span>
</div>
</body>
</html>
欢迎指点,我是新手,有不对的地方可以改进的希望大家指点指点,谢谢!