java tomcat utf-8编码问题 [英] java tomcat utf-8 encoding issue

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

问题描述

我正在使用java / jsp / tomcat / mysql开发一个简单的Web应用程序,最大的问题在于字符编码,因为我需要处理UTF-8编码而不是默认的8851。

I am developing a simple web application using java/jsp/tomcat/mysql, and the most problem lies on the character encoding because I need to deal with UTF-8 encoding instead of the default 8851.

首先,我想描述一下我的程序结构。我正在使用一个名为Controller.java的Servlet来处理所有请求。所以在web.xml中,我有一个Controller servlet,它从* .do获取所有请求。

First of I'd like to describe my program structure. I am using a Servlet called Controller.java to handle all request. So in web.xml, I have a Controller servlet which takes all request from *.do.

然后这个Controller将根据请求的URL调度请求,例如,如果客户端要求register.do,Controller会将请求发送到Register.java。

Then this Controller will dispatch the request based on the requested URL, for example, if client asks for register.do, Controller will dispatch the request to Register.java.

在Register.java中,有一个方法将请求作为参数,即:

And in the Register.java, there is a method which takes the request as parameter, namely:

public String perform(HttpServletRequest request) {
    do something with the request...
}

所以问题是如果我想在这个方法中打印UTF-8中的东西,它会给出随机字符。例如,我有一个存储几个常量的枚举,其中一个属性是Enum在繁体中文中的名称。如果我打印它

So the problem is if I want to print something in UTF-8 inside this method, it will give random characters. For example, I have an Enum which stores several constants, one of the properties the Enum has is its name in Traditional Chinese. If I print it in

public static void main(Stirng[] args{
    System.out.println(MyEnum.One.getChn());
    logger.info(MyEnum.One.getChn());
}

这是用中文正确打印的。但是,如果我把确切的代码放在处理HttpServletRequest的方法中:

This is printed correctly in Chinese. However, if I put the exact code inside the method dealing with HttpServletRequest:

public String perform(HttpServletRequest request) {
    System.out.println(MyEnum.One.getChn());
    logger.info(MyEnum.One.getChn());
}

它们是随机字符打印的,但我可以从调试窗口看到(eclipse )变量保存正确的中文字符。

They are printed as random characters, but I can see from the debug window (eclipse) that the variables are holding correct Chinese characters.

因此,当我想要存储来自request.getParameter()的值时,会出现同样的情况。在调试窗口中,我可以看到变量保持正确的字符,但是我将其打印出来或尝试将其存储在数据库中,它是随机字符。

So, the same situation happens when I want to store the value from request.getParameter(). In the debug window, I can see the variable is holding correct characters, but one I print it out or try to store it in the database, it is random characters.

我不知道WH行为就像这样,这阻止我阅读提交的表单值并将它们存储到数据库中。有人可以给出一些暗示吗?

I don't know why the behavior acts like this, and this is blocking me from reading submitted form values and store them into database. Could someone give some hints on this?

非常感谢。

推荐答案

如果你需要使用UTF-8编码(事实上,现在每个人都应该这样做),那么你可以按照Tomcat常见问题解答中的UTF-8无处不在:

If you need to use UTF-8 encoding (and really, everybody should be going this these days), then you can follow the "UTF-8 everywhere HOWTO" found in the Tomcat FAQ:

http://wiki.apache.org/tomcat/FAQ/CharacterEncoding #Q8

请记住,您还需要在数据库的文本字段中支持UTF-8。

Remember that you also need to support UTF-8 in your database's text fields.

还要记住,有时在其中打印带有非ASCII字符的字符串到日志文件或控制台可能会受到影响

Also remember that sometimes "printing" a String with non-ASCII characters in it to a log file or the console can be affected by


  1. 输出流的字符编码

  2. 文件阅读器的字符编码(例如cat / less / vi)

  3. 字符编码终端

您可能最好将值写入文件然后使用十六进制编辑或检查内容以确保您获得所需的字节值。

You might be better off writing the values to a file and then using a hex editor to examine the contents to be sure that you are getting the byte values you are looking for.

这篇关于java tomcat utf-8编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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