中文乱码求解

gxjimlmxplay 2011-11-28 08:25:25
大家好,我是新手,向数据库中写入数据,中文乱码,我数据库中改为utf-8了,又写了一个bean专门做转换的,还用了一个过滤器,现在不论是读数据还是写数据,中文都是乱码,求解释:代码如下
bean中主要代码:

public String chStr(String str)
{
if(str==null)
{
str="";
}
else
{
try
{
str=(new String(str.getBytes("ISO-8859-1"),"utf-8")).trim();
System.out.println(str);
}catch(Exception e){System.out.println("str");System.out.println(e.getMessage());}
}
return str;
}

servlet:
package hello;

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;


public class EncoddingFilter implements Filter {

public void destroy() {
// TODO 自动生成方法存根

}

public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
// TODO 自动生成方法存根
arg0.setCharacterEncoding("UTF-8");
arg1.setCharacterEncoding("UTF-8");
arg2.doFilter(arg0, arg1);

}

public void init(FilterConfig arg0) throws ServletException {
// TODO 自动生成方法存根

}


}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>EncoddingFileter</filter-name>
<filter-class>hello.EncoddingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncoddingFileter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

对应代码:注册数据:
<%
String username=ch.chStr(request.getParameter("usernamer"));
String pwd=ch.chStr(request.getParameter("pwd"));
String age0=ch.chStr(request.getParameter("age"));
String sex=ch.chStr(request.getParameter("sex"));
int age=Integer.parseInt(age0);
String sql="insert into huiyuan(name,pwd,age,sex) values('"+username+"','"+pwd+"','"+age+"','"+sex+"')";
int i=db.executeUpdate(sql);
%>

登录正确时的代码:
<%
if(session.getAttribute("login_name")==null)
{
response.sendRedirect("index.html");
}
%>
<jsp:useBean id="db" class="hello.DBconn" scope="page"/>
<jsp:useBean id="ch" scope="page" class="hello.StrConvert"/>

<%
String username=(String)session.getAttribute("login_name");
String sql="Select * from huiyuan where name='"+username+"'";
ResultSet rs=db.executeQuery(sql);
if(rs.next())
{
String namer=ch.chStr(rs.getString("name"));
System.out.println(namer);
%>

<table width="400" height="120"class="tb">
<tr id="trone">
<td id="trone1">您的用户名</td>
<td id="trone2"><%=namer%></td>
</tr>
<tr id="trtwo">
<td id="trtwo1">您的密码为</td>
<td id="trtwo2"><%=rs.getString("pwd")%></td>
</tr>
<tr id="trthree">
<td id="trthree1">您的年龄为</td>
<td id="trthree2"><%=rs.getString("age")%></td>
</tr>
<tr id="trfour">
<td id="trfour1">您的性别为</td>
<td id="trfour2"><%=rs.getString("sex")%></td>
</tr>


<%
}
db.close();
%>
...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
五哥 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jk_3699 的回复:]

注意一点 :用 post 提交时 用 response.setCharsetEncoding();
用 get 提交时 用 str=(new String(str.getBytes("ISO-8859-1"),"utf-8"));
[/Quote]

+1
gxjimlmxplay 2011-11-29
  • 打赏
  • 举报
回复
谢谢大家回复,数据库中写入的是中文了,读取的时候还是问号(就是登录正确的时候),写了bean,把从数据库中读取出来的信息通过bean转换还是不行~过滤器这儿也不灵,求高手点播下,我概念现在比较乱~谢谢,谢谢~
y591174893 2011-11-28
  • 打赏
  • 举报
回复
同意三楼
jk_3699 2011-11-28
  • 打赏
  • 举报
回复
注意一点 :用 post 提交时 用 response.setCharsetEncoding();
用 get 提交时 用 str=(new String(str.getBytes("ISO-8859-1"),"utf-8"));
wang7535067 2011-11-28
  • 打赏
  • 举报
回复
<%
request.setCharacterEncoding("gbk");//直接设置下编码格式试下,
String username=ch.chStr(request.getParameter("usernamer"));
String pwd=ch.chStr(request.getParameter("pwd"));
String age0=ch.chStr(request.getParameter("age"));
String sex=ch.chStr(request.getParameter("sex"));
int age=Integer.parseInt(age0);
String sql="insert into huiyuan(name,pwd,age,sex) values('"+username+"','"+pwd+"','"+age+"','"+sex+"')";
int i=db.executeUpdate(sql);
%>
a3737500 2011-11-28
  • 打赏
  • 举报
回复
页面编码统一了,还有你过滤器怎么写得,一般写了过滤器编码就不会有什么问题了

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧