【问题标题】:URL parameter Decoding not workingURL参数解码不起作用
【发布时间】:2013-05-20 06:44:50
【问题描述】:

在我的 java web 应用程序中,我必须构造一个 URL,然后调用该 URL。在创建 URL 时,我正在使用 URLEncoder.encode("text","UTF-8") 对参数值进行编码但是当我们在接收端获取参数并对其进行解码时 - 它没有正确解码。我尝试将编码值设置为请求属性,这工作正常。但不能按客户要求使用。

编写以下代码来测试来自 Apache commons codec 函数的 URLEncoder & URLDecoder 和 URLCodec。

    StringBuffer sb = new StringBuffer("TestSpecialChar'` ~6 Æ æ  Ç  È  123");
    //String testCharacters = "TestSpecialChar'` ~6 Æ æ  Ç  È  123";
    String testCharacters = sb.toString();
    try {
        String encoded = URLEncoder.encode(testCharacters, "UTF-8");
        System.out.println("URLEncoder : " + encoded);
        System.out.println("URLDecoder : " + URLDecoder.decode(encoded, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    URLCodec urlc = new URLCodec("UTF-8");
    try {
        String encoded = urlc.encode(testCharacters);
        System.out.println("urlc.encode : " + encoded);
        System.out.println("urlc.decode : " + urlc.decode(encoded));            
    } catch (EncoderException ee){
        ee.printStackTrace();
    } catch (DecoderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        String encoded = urlc.encode(testCharacters);
        System.out.println("urlc.encode : " + encoded);
        System.out.println("URLDecoder : " + URLDecoder.decode(encoded, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (EncoderException ee){
        ee.printStackTrace();
    }

此代码完美运行。然后我编写了一个简单的 Web 应用程序,其中有两个 JSP 页面,其中一个使用 URL 中的编码值调用另一个。这没有在接收端显示正确的解码值。这是供您参考的代码。

sender.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page language="java" import="java.net.URLEncoder"%>

<!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=ISO-8859-1">
<title>Sender</title>
<script type="text/javascript">
function send(){
window.location='<%=request.getContextPath()%>/jsp/receiver.jsp?txt=<%=URLEncoder.encode("TestSpecialChar'` ~6 Æ æ  Ç  È  123","UTF-8")%>';
}
</script>
</head>
<body onload="javascript:send();">

</body>
</html>

receiver.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page language="java" import="java.net.URLDecoder"%>

<!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=ISO-8859-1">
<title>Receiver</title>
</head>
<body>
<% 
if (request.getAttribute("encoded") != null){
 URLDecoder.decode(request.getAttribute("encoded").toString(),"UTF-8"); 
}
if (request.getParameter("txt") != null){
%>
<%=URLDecoder.decode(request.getParameter("txt").toString(),"UTF-8")%>
inside if getparameter
<%
}
%>
</body>
</html>

我在浏览器中得到以下输出

TestSpecialChar'` ~6 Æ æ Ç 123 inside if getparameter

而不是

TestSpecialChar'` ~6 Æ æ Ç È 123 inside if getparameter

谁能告诉我测试代码有什么问题以及请求属性和参数之间有什么区别,因为属性被正确解码而参数没有?

Temp solution: 通过执行以下操作解决了它: 1) 创建了一个类,它将用 UTF 8 代码替换某些特定字符。 2) 删除了将字符数据作为 URL 参数传递。 3) 确保对外部 URL 的调用是 UTF-8 编码的。 4) 从外部 URL 接收的任何值或在应用程序中在使用该值之前已解码的任何值。

Correct solution: 为了克服这个问题,应用程序的设计和编码必须考虑到 i18n,并且所有 JSP 页面都使用 UTF-8 编码。

【问题讨论】:

  • 请求参数应该由servlet容器自动解码。你不应该自己使用URLDecoder

标签: jsp urlencode urldecode


【解决方案1】:

您的 URL 将您的文本编码为 UTF-8,但 your JSP pages have these declarations in them:

<%@ page language="java" 
    contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
...
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

如果服务器将页面视为ISO-8859-1 instead of UTF-8,这可能会干扰您的编码。将您的 pageEncoding 更改为 UTF-8,看看是否能解决您的问题。

【讨论】:

  • 我在 sender.jsp 和 receiver.jsp 中将字符集更改为 UTF-8,但它仍然没有显示正确的解码文本。我看到以下 URL:http://localhost:8081/AskSam/jsp/receiver.jsp?txt=TestSpecialChar%27%60+~6+%EF%BF%BD+%EF%BF%BD++%EF%BF%BD++%EF%BF%BD++123 输出为 TestSpecialChar' ~6 � � � � 123 inside if getparameter `
猜你喜欢
  • 2016-10-27
  • 2023-03-08
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 2012-08-16
相关资源
最近更新 更多