1

I've a form with Primefaces. The header of the xml file looks like that:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

When I send the form, I take the values with HttpServletRequest:

public String handleRequest(HttpServletRequest request) { 
 String shortname = request.getParameter("shortname");
 (...)

Now when shortname contains a umlaute, for example an ü, the umlaute will be saved as UTF-8 encoded. So my ü get saved as Ã1⁄4.

How can I decode it again? All the tutorials use a byte-array, but I haven't one.

I need this variable in a EMail, and it doesn't look really good with some hieroglyphics.

asked Jul 10, 2013 at 13:57
3
  • 2
    What happens if you override the request's encoding before calling getParameter? e.g. call request.setCharacterEncoding("UTF-8");? Commented Jul 10, 2013 at 14:25
  • Haha what. How simple is that?! Perfect! You can it post a a answer and i'll accept it. Commented Jul 10, 2013 at 14:35
  • Re-posted as an answer. Glad it helped. Commented Jul 10, 2013 at 14:41

1 Answer 1

2

You need to tell the HttpServletRequest instance that it's in UTF-8:

public String handleRequest(HttpServletRequest request) { 
 try {
 request.setCharacterEncoding("UTF-8");
 String shortname = request.getParameter("shortname");
 (...)
 }
 catch (UnsupportedEncodingException e) {
 // ...
 }
}
answered Jul 10, 2013 at 14:40
2
  • 1
    Thanks again! BTW, setCharacterEncoding should be surrounded by try/catch :) Commented Jul 10, 2013 at 14:49
  • Yeah you're right. Serves me right for not actually compiling first :) Commented Jul 10, 2013 at 14:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.