java filter 过滤器中文乱码 怎么解决啊?
1、首先编写一个Java类的filter代码。操作步骤:
创新互联公司是一家专注于成都网站设计、成都做网站、外贸网站建设与策划设计,梁子湖网站建设哪家好?创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:梁子湖等地区。梁子湖做网站价格咨询:18980820575
(1)在myeclipse中新建一个java类,
(2)单击“Add”按钮,在弹出来的对话框中“选择接口”文本框中输入Filter,
并选择匹配好的类型javax.servlet
(3)单击“OK”按钮返回"New Java Class"对话,然后单击“Finish”按钮,就可以看到创建的过滤器框架:
过滤器类: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){
//设置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 !-- 初始化参数 --
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
java过滤非法字符的filter
filter代码在pujia12345提供的代码上改的;
jsp页面的编码你设成你自己的,我用的是utf-8。
input.jsp输入后,正常跳转到handle.jsp,而禁词已经被过滤。
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");//需要过滤的参数
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设置属性保存修改后的值
}
}
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
java中如何过滤html的代码
把需要写入数据库的字符通过下面的方法过滤然后再写入 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(); }
希望采纳
标题名称:过滤输入的代码java java过滤器filter
分享路径:http://lswzjz.com/article/dojpejd.html