如何解决UTF-8在java中 [英] How to solve UTF-8 in java

查看:338
本文介绍了如何解决UTF-8在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用

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


$ b


当我使用 request.getParameter(...); 从文本框中获取数据时,它会检索像ÉÉ? öİ。我看到这个问题,当我使用字符不是英语字符。我在tomcat中将 URIEncoding =UTF-8添加到 server.xml 。但它检索了同样的(ÉÉÉÉÉÉÉÉÄÄ°)。如何解决?



谢谢



EDIT b
$ b

感谢您的回答。



以下是我所做的:




  • 我在server.xml中添加了< Connector URIEncoding =UTF-8... />


  • < meta ... charset = utf-8> 标记正常,我尝试 request.setCharacterEncoding(UTF-8);


  • 我也试过了< filter> 在web.xml中的标记



这些操作都无法解决问题。我想知道这是否有其他错误...(记住:我使用< form method ='post'> 。点击提交按钮,当我使用 request.getParameter(..)获取数据的格式不正确。



执行以下操作:



1)HTML代码

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

2)IE
的浏览器设置View - Encoding - Unicode(UTF-8)



3)Tomcat服务器
server.xml - 在连接器标记中添加了URIEncoding属性

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

catalina.sh/catalina.bat - p>

  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类型的响应应该是application / x-www-form-urlencoded


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. )

解决方案

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"

这篇关于如何解决UTF-8在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆