user1318538
user1318538

Reputation: 341

Java servlet's parameter values are not decoded

I'm having some problem with java servlet's getParameter() which does not decode param even though I've set Tomcat's encoding properly in server.xml.

 <Connector port.. URIEncoding="UTF-8"/>

If I decode raw query I get the decoded query but getParamter does not decode by itself!

 protected void service(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException {
    System.out.println("CharacterEncoding: "+ request.getCharacterEncoding());
    System.out.println("Query String: "+ URLDecoder.decode(request.getQueryString(), "UTF-8");
    System.out.println("Query param name: "+request.getParameter("name"));
    ...

The result I get is as follows:

CharacterEncoding: UTF-8

Query String: name=日本語一番ぜソFOX_&'">•«Ç€Ö™»_αß_iİıI_Администратор_cœur d´Ouchy_𠀀𠄂𪛖_عربي

Query param name: æ¥æ¬èªä¸çªãã½ï¼¦ï¼¯ï¼¸_&'">â¢Â«Ãâ¬Ãâ¢Â»_αÃ_iİıI_ÐдминиÑÑÑаÑоÑ_cÅur d´Ouchy_ð ð ðª_عربÙ

you can clearly see the query and name's value are not same ! In my jsp page I'm using <%@page contentType="text/html; charset=UTF-8" %>

Upvotes: 1

Views: 4324

Answers (1)

BalusC
BalusC

Reputation: 1108632

I understand that this concerns a GET request. Setting <Connector URIEncoding="UTF-8"> should do it. That it doesn't work can only mean that you're running Tomcat from inside an IDE like Eclipse and that the IDE isn't been configured to take over Tomcat's own configuration while you've edited Tomcat's own configuration in /conf/server.xml.

It's unclear which IDE you're using, but if it were Eclipse, you'd need to either edit the server.xml file in the workspace's Servers project instead, not Tomcat's own /conf/server.xml file

enter image description here

Or configure Eclipse to take control of Tomcat's installation by doubleclicking the Tomcat server entry in Servers view and changing the Server Locations section accordingly.

enter image description here

Back to your investigation/fixing attempts: the request.getCharacterEncoding() isn't been used to decode GET query strings (as that's beyond the control of the Servlet API), it's only been used to decode POST request bodies. The <%@page pageEncoding="UTF-8"%> will only set the character encoding of the response and the subsequent form submits.

See also

Upvotes: 2

Related Questions