Jsp URL参数解码不起作用

Jsp URL参数解码不起作用,jsp,urlencode,urldecode,Jsp,Urlencode,Urldecode,在我的JavaWeb应用程序中,我必须构造一个URL,然后调用该URL。创建URL时,我使用urlcoder.encode(“text”,“UTF-8”)对参数值进行编码,但当我们在接收端获取参数并对其进行解码时,它没有正确解码。我尝试将编码的值设置为请求属性,这很好。但不能按客户要求使用 编写了以下代码来测试ApacheCommons编解码器函数中的URLEncoder&URLDecoder和URLCodec StringBuffer sb = new StringBuffer("Te

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

编写了以下代码来测试ApacheCommons编解码器函数中的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>
<%@ 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>

发件人
函数send(){
window.location='/jsp/receiver.jsp?txt=';
}
receiver.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>
<%@ 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>

接受者
内部if getparameter
我在浏览器中获得以下输出

TestSpecialChar'`~6'†如果getparameter

而不是

TestSpecialChar'`~6ÆæÈ123内部if getparameter

有人能告诉我测试代码中有什么错误,请求属性和参数之间有什么区别,因为属性被正确解码,而参数没有

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

正确的解决方案:

为了解决这个问题,应用程序的设计和编码必须考虑i18n,所有JSP页面都必须使用UTF-8编码。

您的URL将文本编码为UTF-8,但是:


...

如果服务器将页面视为一个文件,则这可能会干扰您的编码。将您的
pageEncoding
更改为UTF-8,看看这是否解决了您的问题。

您的URL将文本编码为UTF-8,但:


...

如果服务器将页面视为一个文件,则这可能会干扰您的编码。将
pageEncoding
更改为UTF-8,看看这是否解决了您的问题。

我将sender.jsp和receiver.jsp中的字符集都更改为UTF-8,但仍然没有显示正确的解码文本。我看到了以下网址:
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
如果getparameter`I将sender.jsp和receiver.jsp中的字符集都更改为UTF-8,则输出为
TestSpecialChar'
~6ï½ïï½ïï½ï½123,但它仍然没有显示正确的解码文本。我看到了以下网址:
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
如果getparameter`请求参数应由servlet自动解码,则输出为
TestSpecialChar'
~6ë½ë½ë½ë½123容器。您不必自己使用
urldecker
。请求参数应该由servlet容器自动解码。你不应该自己使用
urldecker