分享JavaWeb中filter过滤器的案例妙用 - 脏话过滤/编码过滤/代码过滤

 

案例1. 利用Servlet的过滤器Filter进行完成脏话过滤

package cn.javabs.filter;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class DirtyWordsFilterDemo implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request;
		HttpServletResponse response;
		try{
			request = (HttpServletRequest)req;
			response = (HttpServletResponse)resp;
		}catch(Exception e){
			throw new RuntimeException("non-http request");
		}
		DirtyWordHttpServletRequest dwrequest = new DirtyWordHttpServletRequest(request);
		chain.doFilter(dwrequest, response);
		

	}

	public void destroy() {

	}

}
class DirtyWordHttpServletRequest extends HttpServletRequestWrapper{
	private String strs[] = {"傻逼","畜生","我去年买了个包"};
	private HttpServletRequest request;
	public DirtyWordHttpServletRequest(HttpServletRequest request){
		super(request);
		this.request = request;
	}
	@Override
	public String getParameter(String name) {
		String value = request.getParameter(name);
		if(value==null)
			return null;
		for(String s:strs){
			if(value.contains(s)){
				value = value.replace(s, "**");
			}
		}
		return value;
	}
	
}

案例2. 利用Servlet的过滤器Filter进行完成Html编码过滤

package cn.javabs.filter;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class HtmlFilterDemo implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request;
		HttpServletResponse response;
		try{
			request = (HttpServletRequest)req;
			response = (HttpServletResponse)resp;
		}catch(Exception e){
			throw new RuntimeException("non-http request");
		}
		
		HtmlHttpServletRequest hrequest = new HtmlHttpServletRequest(request);
		chain.doFilter(hrequest, response);

	}

	public void destroy() {

	}

}
class HtmlHttpServletRequest extends HttpServletRequestWrapper{
	private HttpServletRequest request;
	public HtmlHttpServletRequest(HttpServletRequest request){
		super(request);
		this.request = request;
	}
	@Override
	public String getParameter(String name) {
		String value = request.getParameter(name);
		if(value==null)
			return null;
		value = htmlFilter(value);
		return value;
	}
	private String htmlFilter(String message) {
		if (message == null)
            return (null);

        char content[] = new char[message.length()];
        message.getChars(0, message.length(), content, 0);
        StringBuffer result = new StringBuffer(content.length + 50);
        for (int i = 0; i < content.length; i++) {
            switch (content[i]) {
            case '<':
                result.append("&lt;");
                break;
            case '>':
                result.append("&gt;");
                break;
            case '&':
                result.append("&amp;");
                break;
            case '"':
                result.append("&quot;");
                break;
            default:
                result.append(content[i]);
            }
        }
        return (result.toString());
	}
	
}

案例3. 利用Servlet的过滤器Filter进行完成字符编码过滤(Get及Post方式)

	package cn.javabs.filter;

	import java.io.IOException;
	import java.io.UnsupportedEncodingException;

	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.http.HttpServletRequest;
	import javax.servlet.http.HttpServletRequestWrapper;
	import javax.servlet.http.HttpServletResponse;
	/**
       * 同时解决GET及POST请求参数和响应输出的编码过滤器
       */
	public class SetAllEncodingFilterDemo implements Filter {
		private FilterConfig filterConfig;
		public void init(FilterConfig filterConfig) throws ServletException {
			this.filterConfig = filterConfig;
		}

		public void doFilter(ServletRequest req, ServletResponse resp,
				FilterChain chain) throws IOException, ServletException {
			HttpServletRequest request;
			HttpServletResponse response;
			try{
				request = (HttpServletRequest)req;
				response = (HttpServletResponse)resp;
			}catch(Exception e){
				throw new RuntimeException("non-http request");
			}
			String encoding = "UTF-8";
			String value = filterConfig.getInitParameter("encoding");
			if(value!=null){
				encoding = value;
			}
			
			request.setCharacterEncoding(encoding);//POST请求方式
			response.setContentType("text/html;charset="+encoding);
			MyHttpServletRequest mrequest = new MyHttpServletRequest(request);
			chain.doFilter(mrequest, response);
		}

		public void destroy() {

		}

	}
	class MyHttpServletRequest extends HttpServletRequestWrapper{
		private HttpServletRequest request;
		public MyHttpServletRequest(HttpServletRequest request){
			super(request);
			this.request = request;
		}
		public String getParameter(String name) {
			String value = request.getParameter(name);
			if(value==null)
				return value;
			try {
				if("get".equalsIgnoreCase(request.getMethod()))
					value = new String(value.getBytes("ISO-8859-1"),request.getCharacterEncoding());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return value;
		}
		
	}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值