在JSP页面中:
<%!String trans(String chi)
{//设置汉字转换
String result=null;
byte temp[];
try{
temp=chi.getBytes("iso-8859-1");
result=new String(temp);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return result;
}
%>
在JAVA程序中:
public String trans(String chi)
{
String result=null;
byte temp[];
try{
temp=chi.getBytes("iso-8859-1");
result=new String(temp);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return result;
}
=============================================
A.JSP在此页面的Form的method属性若设置成post,即<form name="form1" method="post" action="B.JSP">.则在B.JSP页面只要用<%request.setCharacterEncoding("iso-8859-1");%>便可解决由于request.getParameter()获取前页数据出现的中文乱码问题。
若前一页面的<form method="get" name="form1" aciotn="b.jsp">只要在b.jsp设置request.setCharacterEncoding("GB2312")。
若插入数据到数据库,插入数据库中的数据有中文乱码。只要把数据库中可能出现汉字的数据类型如char、varchar等设置成nchar、nvarchar。依上例方案解决。
当然,最好的处理乱码方法个人建议还是使用servlet过滤器,非常好用