java网页中url传值的转码

为了避免中文乱码,也为了避免是传递的值在地址栏中被一目了然的看见,所以我们需要在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>

运行结果:

 

转载于:https://www.cnblogs.com/hehejeson/articles/6390956.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值