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:32

UTF 8 should be set at all the layers of the application.

Do following

1) HTML Code

 <meta contentType="text/html; charset="UTF-8"/>

2) Browser Setting for IE View -- Encoding -- Unicode (UTF-8)

3) Tomcat Server server.xml - In Connector tag added "URIEncoding" attribute as

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" URIEncoding="UTF-8"/>

catalina.sh/catalina.bat - added following

set JAVA_OPTS=--Xms256m -Xmx1024m -Xss268k -server -XX:MaxPermSize=256m -XX:-UseGCOverheadLimit -Djava.awt.headless=true -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8

set CATALINA_OPTS=-Dfile.encoding="UTF-8"

4) MIME type of response should be "application/x-www-form-urlencoded"

查看更多
登录 后发表回答