jsp url传中文最简单正确的方法

来源:互联网 发布:windows phone 10应用 编辑:程序博客网 时间:2024/05/19 13:18

在jsp中,如果在url中传递中文时会出现乱码,在网上关于这一问题的解决方法五花八门,但都不是很奏效!其实解决方法非常简单: 第一步:编码(以传递的参数为str为例) 在传递数据前将str进行编码Java.net.URLEncoder.encode(str) 第二步:解码 在获取数据端将得到的数据进行解码 new String(str.getBytes("ISO8859_1"))

简单示例程序如下:

<%@ page contentType="text/html;charset=gb2312" %>

<a href="ds.jsp?url=<%=java.net.URLEncoder.encode("编码的是这里","GB2312")%>">点击这里</a>


<%
//request.setCharacterEncoding("GBK");
if(request.getParameter("url")!=null)
{

str=request.getParameter("url");

//下面是解码
str=java.net.URLDecoder.decode(str,"GB2312");
str=new String(str.getBytes("ISO-8859-1"));
out.print(str);
}

%>

 

 

来自:http://blog.csdn.net/fhm727/archive/2009/09/03/4513811.aspx