1

In my java web application I have to construct a URL and then call the URL. While creating the URL I am encoding the parameter value using URLEncoder.encode("text","UTF-8") But when we get the parameter at the receiving end and decode it - it is not decoding properly. I tried setting the encoded value as request attribute this works fine. But can't use it as per client request.

Wrote following code to test URLEncoder & URLDecoder and URLCodec from Apache commons codec functions.

    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();
    }

This code works perfectly. Then I wrote a simple web application where I have two JSP page in which one is calling other with a encoded value in the URL. This is not showing the proper decoded value at the receiver end. Here is the code for your reference.

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>

I get following output in the browser

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

instead of

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

Can someone please let me know what is wrong in test code and what is difference between request attribute and parameter due to which the attribute is decoded properly whereas parameter does not?

Temp solution: Solved it by doing following things: 1) Created a class which would replace some specific characters with UTF 8 code. 2) Removed passing of character data as URL parameters. 3) Making sure that call to external URL is UTF-8 encoded. 4) Any value received from external URL or which in the application is decoded before using the value.

Correct solution: To over come this problem the application has to be designed and coded with i18n in mind and all the JSP page with UTF-8 encoding.

Natraj
  • 397
  • 4
  • 9
  • 35
  • The request parameters should be decoded automatically by the servlet container. You shouldn't have to use `URLDecoder` yourself. – user207421 Dec 31 '16 at 06:30

1 Answers1

1

You URL encode your text as UTF-8 but 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">

That might be interfering with you encoding if the server treats the page as ISO-8859-1 instead of UTF-8. Change your pageEncoding to UTF-8 and see if that fixes your issue.

Community
  • 1
  • 1
Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • 1
    I changed the charset to UTF-8 in both the sender.jsp and receiver.jsp but still it is not showing the proper decoded text. I am seeing following 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` And the output as `TestSpecialChar'` ~6 � � � � 123 inside if getparameter ` – Natraj May 21 '13 at 04:44