Michael Schmidt
Michael Schmidt

Reputation: 9230

Decode Java Char / String

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.

Upvotes: 1

Views: 440

Answers (1)

Seth Davenport
Seth Davenport

Reputation: 389

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) {
       // ...
   }
}

Upvotes: 2

Related Questions