Tuesday, March 1, 2011

Additional Ascii char 'Â' added in JSP request

Hi,

I am facing the same issue in tomcat & jsp as listed in below asp issue http://stackoverflow.com/questions/350429/classic-asp-gremlims-getting-a-ainserted-into-text-whenever-an-html-special-cha

Using tomcat 5 and jsp. When I type in a string containing like ±20°C and submit to another JSP the resultant parameter after submit is ±20°C. An addition character 'Â' is being added before every special char in the request itself. How do I resolve this issue?

Thanks,

From stackoverflow
  • That is the symptom of ISO-Latin-whatever source data being transcoded into UTF-8 on the way out. Check your character encodings.

  • What should be correct encoding? Where and how should it be set for this issue?

    Bozho : use comments instead of answers.
  • Try

    <%@ page pageEncoding="utf-8" %>
    

    and

    request.setCharacterEncoding("utf-8");
    
  • I had a similar problem, which seems to now be fixed after altering the following <meta> line in the <head> block.

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    

    It's the utf-8 which is important!

    Hope that helps.

  • This is caused by displaying UTF-8 page as Latin-1. For example, ± is encoded as 0xB1 in Latin-1 but 0xC2, 0xB1 in UTF-8. 0xC2 happens to be Â.

    This is kind of strange for a JSP page. Normally, JSP will use the same encoding in the writer and "Content-Type" header so you always get the same encoding. Check if you specifies encoding like this,

    <%@page pageEncoding="UTF-8" %>
    

    If you have a custom "Content-Type" header, make sure you append ", charset=UTF-8".

  • To the point: during each step where byte-char conversion is involved, you need to specify the same and correct character encoding everywhere. To cover any human character at the world, UTF-8 is a perfect choice.

    There are several steps you need to specify this character encoding: parsing GET query parameters (configureable in appserver settings), parsing POST request body (configureable with help of a Filter), writing to the response stream (configurable with help of @page pageEncoding in JSP or HttpServletResponse#setCharacterEncoding() in Servlet), reading/writing to database (configureable in DB table itself), reading/writing to files (configureable using InputStreamReader and OutputStreamWriter).

    You can find more detailed background information and complete code solutions in this article.

    Hope this helps.

0 comments:

Post a Comment