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.