1、首先編寫一個Java類的filter代碼。操作步驟:
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計,梁子湖網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:梁子湖等地區(qū)。梁子湖做網(wǎng)站價格咨詢:18980820575
(1)在myeclipse中新建一個java類,
(2)單擊“Add”按鈕,在彈出來的對話框中“選擇接口”文本框中輸入Filter,
并選擇匹配好的類型javax.servlet
(3)單擊“OK”按鈕返回"New Java Class"對話,然后單擊“Finish”按鈕,就可以看到創(chuàng)建的過濾器框架:
過濾器類:Encoding.java,代碼如下:
package com;
import java.io.IOException;
import javax.servlet.*;
public class Encoding implements Filter {
protected String encoding=null;
protected FilterConfig config;
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request.getCharacterEncoding()==null){
//得倒指定的編碼
String encode=getEncoding();
if(encode!=null){
//設(shè)置request的編碼
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.config=filterConfig; //得到web.xml中的配置編碼
this.encoding=filterConfig.getInitParameter("Encoding");
}
protected String getEncoding(){
return encoding;
}
}
2、在web.xml文件寫入以下代碼:
?xml version="1.0" encoding="UTF-8"?
web-app version="2.5"
xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
"
display-name/display-name
filter !-- 控制編碼 --
filter-nameEncodingFilter/filter-name
filter-classcom.Encoding/filter-class
init-param !-- 初始化參數(shù) --
param-nameEncoding/param-name
param-valueGB2312/param-value
/init-param
/filter
filter-mapping
filter-nameEncodingFilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
/web-app
filter代碼在pujia12345提供的代碼上改的;
jsp頁面的編碼你設(shè)成你自己的,我用的是utf-8。
input.jsp輸入后,正常跳轉(zhuǎn)到handle.jsp,而禁詞已經(jīng)被過濾。
filter:
package test;
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyFilter implements Filter
{
private ListString unString;
public void init(FilterConfig filterConfig) throws ServletException
{
unString = new ArrayListString();
unString.add("日");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
String content = request.getParameter("content");//需要過濾的參數(shù)
if(content!=null){
for (int i = 0; i unString.size(); i++)
{
String strIllegal = unString.get(i);
if (content.indexOf(strIllegal) = 0)
{
content = content.replaceAll(strIllegal, "");//非法字符替換成空
}
request.setAttribute("content", content);//為request設(shè)置屬性保存修改后的值
}
}
chain.doFilter(request, response);
}
public void destroy()
{
//System.out.println("過濾器銷毀");
}
}
//---------------------------//
web.xml:
filter
filter-namemyfilter/filter-name
filter-classtest.MyFilter/filter-class
/filter
filter-mapping
filter-namemyfilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
//---------------------------//
輸入頁面input.jsp:
%@page contentType="text/html;charset=utf-8"%
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
titleinput.jsp/title
/head
body
form action="handle.jsp" method="post"
input type="text" name="content" /
input type="submit" value=" 提交 " /
/form
/body
/html
//---------------------------//
input提交的頁面handle.jsp:
%@page contentType="text/html;charset=utf-8"%
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
title handle.jsp /title
/head
body
%
String content = (String)request.getAttribute("content");
out.println(content);
%
/body
/html
把需要寫入數(shù)據(jù)庫的字符通過下面的方法過濾然后再寫入 public static String converthtml(String input) { if (input == null || input.length() == 0) { return input; } StringBuffer buf = new StringBuffer(input.length() + 6); char ch = ' '; for (int i = 0; i input.length(); i++) { ch = input.charAt(i); if (ch == '') { buf.append(""); } else if (ch == '') { buf.append(""); } else if (ch == '') { buf.append(""); } else if (ch == ' ') { buf.append("?"); } else { buf.append(ch); } } return buf.toString(); }
希望采納