开发者

Additional Ascii char 'Â' added in JSP request

开发者 https://www.devze.com 2022-12-13 13:51 出处:网络
I am facing the same issue in tomcat & jsp as listed in below asp issue Classic ASP gremlims, getting a  inserted into text whenever an HTML special character is used

I am facing the same issue in tomcat & jsp as listed in below asp issue Classic ASP gremlims, getting a  inserted into text whenever an HTML special character is used

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开发者_如何学C. How do I resolve this issue?

Thanks,


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".


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号