1

I'm using JSP on a Apache Tomcat 7.0.41 and want to transfer Form-Data to another page.

For some reasons the German "Umlaute" work if I use GET, but don't if I use POST. Examples:

@€ --> @â¬

äöüß --> äöüÃ

Österreich --> Ãsterreich

So I wrote a small Script which uses the recommended encodeURIComponent()-JS-Function:

function onsubmitfu() {
            tas = document.getElementsByTagName('textarea');
            for (index = 0; index < tas.length; index++) {
                tas[index].innerHTML = encodeURIComponent()(tas[index].innerHTML);
            }
            tas = document.getElementsByTagName('input');
            for (index = 0; index < tas.length; index++) {
                tas[index].value = encodeURIComponent(tas[index].value);
            }
        }

And well, it "changes"...

äöü߀ --> äöüÃâ¬

Some sources on the Internet prefer escape() so I gave it a try and it works how it is supposed to work but on the wrong side... Now the JSP-Page receives:

äöü߀ --> %E4%F6%FC%DF%u20AC

This looks how it would be supposed to look in the URL I guess...

Both pages use

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Yet I don't understand why there are differences in the behavior of these two variants. Is there a workaround?

Community
  • 1
  • 1
Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • It seems `request.setCharacterEncoding("UTF-8");` is important to always put before the first reading of the request-Parameters. A couple of days before I had the same issue but used this command to dowdy. Now it works. The earlier you put it the better it is... – Qohelet May 03 '14 at 21:46

1 Answers1

1

Is your Tomcat server set to handle UTF8 characters? This is how I do it in Tomcat 6 on Ubuntu 12.04. This should give you an idea of how to handle.

First, I open up the main Tomcat server XML file like so:

sudo nano /etc/tomcat6/server.xml

Then I look for the <Connector section and make sure it contains URIEncoding="UTF-8". It looks like this by default:

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" />

But should look something like this when you add the URIEncoding="UTF-8" to it:

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           URIEncoding="UTF-8"
           redirectPort="8443" />

Then restart Tomcat 6 like so:

sudo service tomcat6 restart.

And see what happens. Additionally, please see the suggestions in this question and answer thread. This programatic answer seems to address the issue:

if(request.getCharacterEncoding() == null) {
   request.setCharacterEncoding("UTF-8");
}
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • You were completely right related to the Tomcat. (I just found it in a different directory: /apache-tomcat-7.0.41/conf$ ) Unfortunately there are no changes :-/ On regular submit the results remain the same and encodeURIComponent behaves like "escape"... – Qohelet May 03 '14 at 19:09
  • 1
    Happy to hear you were able to do something! But sorry you cannot solve the core issue. Perhaps this question/answer thread can help? http://stackoverflow.com/questions/374573/character-encoding-jsp-displayed-wrong-in-jsp-but-not-in-url-a-a-e-a?rq=1 – Giacomo1968 May 03 '14 at 19:12
  • if(request.getCharacterEncoding() == null) { request.setCharacterEncoding("UTF-8"); } Solved it!! I guess the combination made it. Thank you!!! – Qohelet May 03 '14 at 19:25