开发者

Can not set Character Encoding using sun-web.xml

开发者 https://www.devze.com 2022-12-23 03:55 出处:网络
I am trying to send special characters like spanish chars from my page to a JSP page as a form parameter. When I try get the parameter which I sent, it shows that as \"?\" (Question mark).

I am trying to send special characters like spanish chars from my page to a JSP page as a form parameter. When I try get the parameter which I sent, it shows that as "?" (Question mark). After searching on java.net thread I came to know that I should have following entry in my sun-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 8.0 Servlet 2.4//EN" "http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_4-0.dtd">
<sun-web-app>
<locale-charset-info default-locale="es">
<locale-charset-map locale="es" charset="UTF-8开发者_运维知识库"/>
<parameter-encoding default-charset="UTF-8"/>
</locale-charset-info>
</sun-web-app>

But it did not work with this approach still the character goes as "?".


Try this one, also make sure to encode the character in UTF-8 before sending them to form or database.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 8.0 Servlet 2.4//EN" "http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_4-0.dtd">

<sun-web-app>
<locale-charset-info default-locale="">
<locale-charset-map locale="" charset=""/>
<parameter-encoding default-charset="UTF-8"/>
</locale-charset-info>
</sun-web-app>

This makes server to enforce the character encoding to whatever you have set.


You've only configured the request parameter encoding. As per the symptoms, you need to configure the response encoding as well. In a JSP page it's usually sufficient to add the following line to the top:

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

More background information and technical solutions can be found here.

When you're using a database, another possible cause is that the database is not configured to use UTF-8 so that the characters are already stored as garbage and whatever way you query it, it will always return garbage. Review it with an independent DB admin tool. If this is indeed the case, then you need to set the database table encoding as well.

0

精彩评论

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

关注公众号