首页 > 代码库 > java的url有中文参数导致乱码

java的url有中文参数导致乱码

话不多说,直接粘代码

发送方

UrlParaCode.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
  <head>
 	<title>My JSP ‘UrlParaCode.jsp‘ starting page</title>
  </head>
  <body>
  <%
  	String UrlPara="王屯屯";
   %>
   <a href=http://www.mamicode.com/"UrlParaCodeAfter.jsp?UrlPara=">这是一个url带有中文参数>
接收方

UrlParaCodeAfter.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<html>
  <head>

    <title>My JSP ‘ButttonUrlParaCodeAfetr.jsp‘ starting page</title>


  </head>
  
  <body>
    <%
    	request.setCharacterEncoding("GBK");
    	response.setCharacterEncoding("GBK");
    	String UrlPara=request.getParameter("UrlPara");
    	String DecodePara = new String(UrlPara.getBytes("ISO-8859-1"), "GBK");
    	out.println("UrlPara"+UrlPara);
    	out.println("DecodePara"+DecodePara);
     %>
  </body>
</html>