简体   繁体   English

解码Java字符/字符串

[英]Decode Java Char / String

I've a form with Primefaces. 我有一张Primefaces表格。 The header of the xml file looks like that: xml文件的标题如下所示:

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

When I send the form, I take the values with HttpServletRequest : 发送表单时,我使用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. 现在,当shortname包含一个umlaute,例如ü时 ,该umlaute将被保存为UTF-8编码。 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. 我在电子邮件中需要此变量,但对于某些象形文字来说,它看起来并不好。

You need to tell the HttpServletRequest instance that it's in UTF-8: 您需要告诉HttpServletRequest实例它在UTF-8中:

public String handleRequest(HttpServletRequest request) {    
   try {
       request.setCharacterEncoding("UTF-8");
       String shortname = request.getParameter("shortname");

       (...)
   }
   catch (UnsupportedEncodingException e) {
       // ...
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM