How can I read GET or POST parameters that were encoded in an international character set? Also, when a user fills in an HTML Form using a custom Input Method Editor for, say, Japanese, how can my servlet/JSP know which encoding was used?
Created May 4, 2012
Jan Borchers
The HttpServletRequest object from 2.0 on provides a method for retrieving the client's character encoding. The follwing sample gets the parameter mytext
and converts it to a java Unicode string.
public String getText(HttpServletRequest request) { String value = request.getParameter("mytext"); try{ value = new String(value.getBytes(), request.getCharacterEncoding()); }catch(java.io.UnsupportedEncodingException ex){ System.err.println(ex); } return value; }
See also