0

From jsp, in text box, I am entering "göteborg". But in my servlet when I do a "request.getParameter" I get "göteborg".

Is this behavior correct?

I have following settings in jsp page.

<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<meta charset="UTF-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

I have also set following line in servlet before I make call to request.getParameter:

request.setCharacterEncoding("UTF-8");

What is missing?

Icarus3
  • 2,310
  • 14
  • 22
  • Can you check the request/response headers on the original request, and the headers for the request in question. – Boris the Spider Mar 15 '15 at 12:03
  • what matters is the charset seen by the browser when it received the form. – bmargulies Mar 15 '15 at 12:05
  • The explanation is that the form post request from the browser to your server is independent. HTTP default encoding is ISO-8859-1, so there is a missing `request.setEncoding("UTF-8");` There are fortunately solutions to do this encoding of the incoming request at one place. – Joop Eggen Mar 15 '15 at 12:40
  • Thank you guys, I solved the problem by adding URIEncoding="UTF-8" to 8080 connector in server.xml. – Icarus3 Mar 15 '15 at 12:42
  • You might want to self-answer the question then - to provide closure and in case anyone else runs into the same problem. – Boris the Spider Mar 16 '15 at 09:52

2 Answers2

1

If you are using glassfish, you can try adding this line to your web.xml:

<parameter-encoding default-charset="UTF-8"/>

Also, there's some good advice from Tomcat's character encoding FAQ (although it applies to any servlet container):

  • Use a character encoding filter with the default encoding set to UTF-8 Change all your JSPs to include charset name in their contentType.

  • Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8.

NotGaeL
  • 8,344
  • 5
  • 40
  • 70
  • 1
    Thanks, actually, parameter-encoding did not help ( I am using tomcat 6 ). But I got pointer from the FAQ link that you shared. I had to set URIEncoding="UTF-8" in server.xml for 8080 connector and that solved my problem. – Icarus3 Mar 15 '15 at 12:40
  • Yeah, that FAQ has been really useful to me too when using Tomcat :-) I've mentioned `parameter-encoding` then I realized it's just for Glassfish, other implementations (such as Tomcat) differ (Anyway being the reference implementation and all, it's still good to know). Anyway all of them have in common what you already had done and those other two tips from Tomcat's FAQ. – NotGaeL Mar 15 '15 at 12:49
0

( Answering the question to provide closure ).

The default encoding in Tomcat for request/response is ISO-8859-1 ( As required by servlet specification)

Ref: http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

I solved the problem by setting URIEncoding="UTF-8" in server.xml for 8080 connector.

Please refer to all the comments provided by these nice people.

Icarus3
  • 2,310
  • 14
  • 22