乱码的问题!

casear_huang 2002-05-24 05:59:44
我写了一个非常简单的表单提交页面test3.jsp,可是输入中文提交,显示就是乱码,我在一本书上看到一个解决方法写了一个getStr,可是还是乱码,这个问题该如何解决?

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<center>
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{

}
return "null";
}
%>

<%!
String username;
%>
<%
username =request.getParameter("username");
username = getStr(username);
out.println (username);
%>
<form name="form1" method="post" action="test3.jsp">
<p>姓名:
<input type="text" name="username">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<p> </p>
</center>
</body>
</html>
...全文
36 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andrawu 2002-05-24
  • 打赏
  • 举报
回复
如上一句
<%@ page contentType="text/html;charset=gb2312"%>
wjfxiao 2002-05-24
  • 打赏
  • 举报
回复
直接用
username = new String(username.getBytes("ISO-8859-1"),"gb2312");
就行!
jspxnet 2002-05-24
  • 打赏
  • 举报
回复
下次有问题先搜索一下前边的帖。

public static String unicodeToGB(String strIn){
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}catch(UnsupportedEncodingException e){}
return strOut;
}

public static String GBToUnicode(String strIn){
     String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))return strIn;
     try{
       byte[] b = strIn.getBytes("ISO8859_1");
       strOut = new String(b,"GBK");
     }catch(Exception e){}
    return strOut;
}
_______________________________________________
<%String sPara = request.getParameter("para") ;
String s = new String(sPara.getBytes("8859_1"),"gb2312");
%>
<%=s%>

81,091

社区成员

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

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