How to solve UTF-8 in java

2019-07-21 08:50发布

I currently use

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

in my jsp page.

And when I get data from textbox using request.getParameter("..."); it retrieves data like that öÉ?É?É?öİ . I saw this problem when I used characters that are not english chars. I add URIEncoding="UTF-8" to server.xml in tomcat. But it retrieved the same (öÉ?É?É?öİ). How to solve it?

Thank you

EDIT

Thanks for your answers. I tried a few things, but nothing has fixed the problem.

Here's what I've done:

  • I added <Connector URIEncoding="UTF-8" .../> in server.xml.

  • <meta ... charset=utf-8> tag is ok and I tried request.setCharacterEncoding("UTF-8");

  • I also tried <filter> tag in web.xml

None of these actions fixes the problem. I'm wondering if there's something else wrong with this...(remembering: I used <form method='post'>. I click submit button and when I get data using request.getParameter("..") the format of this data is not the correct format. )

7条回答
太酷不给撩
2楼-- · 2019-07-21 09:09

May be because the actual character encoding is not UTF-8 ? If the characters itself are encoded in some other format then we just can't label them as UTF-8.

Try decoding them by giving various charset and see which one gives proper result. I think the input character encoding is latin1(ISO-8859-1). If yes, follow below code

String param1 = request.getParameter("...");
if(param1!=null)
{
  param1 = new String(param1.getBytes("ISO-8859-1"));
}
查看更多
神经病院院长
3楼-- · 2019-07-21 09:10

You can try this code in your Servlet

if(request.getCharacterEncoding() == null) {
    request.setCharacterEncoding("UTF-8");
}
查看更多
冷血范
4楼-- · 2019-07-21 09:10

You should try it

String content= request.getParameter("content");
if(content!=null)
  content = new String(content.getBytes("ISO-8859-1"));
查看更多
来,给爷笑一个
5楼-- · 2019-07-21 09:12

There is another place you can check. Did you include following declaration in your JSP file?

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

I think the problem is that browser still sends requests using default ISO-8859-1, which is the standard charset if not declared.

You can also check the HTTP headers received from server to make sure the correct charset is sent back.

查看更多
Lonely孤独者°
6楼-- · 2019-07-21 09:26

Essentially the cleanest way to do it is to use Unicode in your property files and/or code if need be (not adviced).

This way you avoid all encoding issues, since your programm only has deal with ASCII code, the proper reprenstation is then handeled entierly by the client side and you do not have to worry about the standard os encoding or enviorment encoding.

查看更多
虎瘦雄心在
7楼-- · 2019-07-21 09:29

You can also try adding the following filter at web.xml:

<filter>
 <filter-name>Character Encoding Filter</filter-name>
 <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
</filter>

Hope this help

查看更多
登录 后发表回答