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 ü.

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.

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) {
       // ...
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again! BTW, setCharacterEncoding should be surrounded by try/catch :)
Yeah you're right. Serves me right for not actually compiling first :)

Your Answer

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.