超级链接传递中文 编码问题
[1]对于
<%
String param="我是我";
%>
由<a href=as.jsp?param=<%=param%>>nihao</a>
传递的中文问题,可以采取在本页面 showpt.jsp 和接收参数的as.jsp页面顶头添加
<%@ page contentType="text/html; charset=GBK" %>
并用
String param= new String(request.getParameter("param").getBytes("ISO-8859-1"), "GBK");
来接收
则会发现,在as.jsp页面的地址栏中出现的是as.jsp?param=我是我
页面也能正确的接收。
[2]表单 method="get" 方法的处理方式 也是如此
//showpt.jsp
<%...--
Document : showpt
Created on : 2008-4-23, 19:22:45
Author : a60
--%>



<%...@ page contentType="text/html; charset=gbk" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<%...
String param="我是我";
%>
<a href=as.jsp?param=<%=param%>>nihao</a>
<form action="as.jsp" method="get">
标题:<input type="text" name="param" value="">
<input type="submit" name ="submit"value="提交" >
</form>
</body>
</html>
//as.jsp
<%...--
Document : as.jsp
Created on : 2008-4-23, 19:30:43
Author : a60
--%>



<%...@ page contentType="text/html; charset=GBK" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>sssss</h2>
<%...
String param= new String(request.getParameter("param").getBytes("ISO-8859-1"), "GBK");

if(param!=null){
out.println(param);
%>
<a href="index.jsp?param=<%=param%>">heheh</a>

<%...
}
//String cont=request.getParameter("cont");
// out.println(cont);
else{
out.println();
}
%>
</body>
</html>
//index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%...@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%...
String param= new String(request.getParameter("param").getBytes("ISO-8859-1"), "GBK");
if(param!=null){
out.println(param);
}
%>
<h2>Hello World!</h2>
</body>
</html>
本文介绍了解决通过超级链接和GET方法传递中文参数时遇到的编码问题的方法。具体步骤包括设置页面字符集为GBK,并使用特定编码转换获取的参数。
2214

被折叠的 条评论
为什么被折叠?



