ITPub博客

首页 > 数据库 > MySQL > mysql与jsp乱码解决方法

mysql与jsp乱码解决方法

原创 MySQL 作者:terryisme 时间:2009-03-05 14:34:57 0 删除 编辑

在数据库连接时添加:Connection Conn=DriverManager.getConnection("jdbc:mysql://localhost /test?useUnicode=true&characterEncoding=gb2312(或utf-8)","root","");

将mysql中的filed charset 属性设置为gbk.

添加类SetEncodingFilter

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;


/** *//**
* 请求中中文字符串过滤类
*/

public class SetEncodingFilter
implements Filter{


public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

request.setCharacterEncoding("gb2312");
chain.doFilter(request,response);
}

public void destroy(){
}
}

配置web.xml


http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


debug
3


detail
3

0


action
*.do


index.jsp


Set Encoding
com.blog.filter.SetEncodingFilter


Set Encoding
/*

[@more@]http://hi.baidu.com/%CC%EC%D1%C4%D2%BB%D0%A6/blog/item/b8e7f3092d175a87d0581bea.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/93029/viewspace-1018124/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-02-23

  • 博文量
    142
  • 访问量
    632458