0

i have a JSP with

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

in the head section, with following code i try to set the content to UTF-8:

<%@page contentType="text/html;charset=utf-8" %>

response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");

Within a form is an input field:

<input type="text" value="LastName*" class="input required" name="lastName" id="lastName" />

I have now problems with german special characters.

When i use request.getParameter("lastName"), this works fine in FireFox, but not in IE.

String encodedLastName = new String(request.getParameter("lastName").getBytes("iso-8859-1"), "UTF-8");

works on IE, but not in Firefox.

I tried to change everything to iso-8859-1, added accept-charset="UTF-8" to the form, ...

Now it is more guessing than working.

Can this only configured within the server (Tomcat) but why there is a difference between the behaviour of the browsers?

Thank, Markus

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • What is actually happening in IE? You're getting the right character just in iso-8859-1, or do you get a question mark? – developerwjk Nov 04 '13 at 22:06
  • In Firefox the wrong character is shown as a white ? on a black diamond, in IE the wrong character is shown as two chars(!), a capital A with an ~ on top (in ISO 8859-1 table it is C3) and a special char, for instance ü (ü) is 1/4 (in ISO 8859-1 table it is BC). The behaviour is the same if i switch in the browsers to another encoding (8859-1). – Markus Ströbel Nov 05 '13 at 09:34
  • Basically you just need your JSP and the HTML meta tag to have the encoding match. Do you have <%@page contentType="text/html;charset=utf-8" %> on the very first line of the JSP? – developerwjk Nov 05 '13 at 21:53
  • Yes, as stated above, i have the <%@page line at the beginning, the – Markus Ströbel Nov 06 '13 at 12:24
  • Ok, i have to change my problem to some jquery issue: When i disable javascript and i use <%@page ... %> and String encodedLastName = new String(request.getParameter("lastName").getBytes("iso-8859-1"), "UTF-8"); this works fine in IE and firefox. But when javascript is enabled, the jsp is called with AJAX like that $.ajax({ type: 'POST', url: jqm('.validForm').attr('action'), data: jqm(".validForm").serialize(), success: function(data){ $('.popupWrapper').hide().html(data).fadeIn(1500); } }); I think the problem lies in a different behaviour in the .serialize() function. – Markus Ströbel Nov 08 '13 at 16:39
  • Try including content-type in your $.ajax(). See http://api.jquery.com/jQuery.ajax/ and http://stackoverflow.com/questions/553463/jquery-ajax-character-encoding-problem – developerwjk Nov 08 '13 at 20:26
  • contentType: 'application/x-www-form-urlencoded; charset=UTF-8', should be default, accoring to api.jquery.com/jQuery.ajax. But after set the contentType by hand, the IE and the FF behaviour is now the same. Problem solved; thanks to all of you! – Markus Ströbel Nov 11 '13 at 10:43

1 Answers1

0

There were two problems which interfere with each other:

1) When using a normal post, i have to encode correct via

<%@page contentType="text/html;charset=utf-8" %>

and decode correct via

String encodedLastName = new String(request.getParameter("lastName").getBytes("iso-8859-1"), "UTF-8");

2) When using jquery, adding

contentType: 'application/x-www-form-urlencoded; charset=UTF-8'

in the $.ajax call.