为了避免中文乱码,也为了避免是传递的值在地址栏中被一目了然的看见,所以我们需要在URL船只的时候进行转码。在.net包内有两个类

(1)URLEncoder类,负责将字符串转化为URL编码,一般使用如下函数:

   public static String encode(String a,String enc)

    throws UnsupportedEncodingExeption

(2)URLDecoder类,负责将URL编码的字符串转换为原编码。一般使用如下函数:

    public static String decode(String s,String enc)

    throws UnsupportedEncodingExeption

 

其中参数1为“传入字符串”。参数2为“编码名称”;

 

 

例:welcome.jsp

 1 <%@ page language="java" pageEncoding="utf-8" %>
 2 <%@ page import="java.net.*" %>
 3 <html>
 4 <body>
 5 <%
 6     String srcStuname="Hello,唐云";
 7     String urlStuname=URLEncoder.encode(srcStuname,"utf-8");
 8 %>
 9 <a href="welcom.jsp?stuname=<%=urlStuname %>">唐云连接</a>
10 <%
11     urlStuname=request.getParameter("stuname");
12     if(urlStuname!=null)
13     {
14         String resStuname=URLDecoder.decode(urlStuname,"utf-8");
15         //resStuname=new String(resStuname.getBytes("gb2312"));
16         out.println(resStuname);
17     }
18 %>
19 <br>
20 </body>
21 </html>

运行结果: