views:

304

answers:

2

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"&gt;
<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 "?".

+1  A: 

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.

BalusC
+2  A: 

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"&gt;

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

So if you leave out the locale and charset map, the request parameter encoding will be actually applied in Glassfish? That seems odd to me.
BalusC