I am developing a simple web application using java / jsp / tomcat / mysql and the main problem is character encoding, because I need to deal with UTF-8 encoding instead of the standard 8851.
First, I would like to describe my program structure. I am using a Servlet called Controller.java to handle the entire request. So, in web.xml, I have a Controller servlet that accepts all requests from * .do.
Then this controller will send the request based on the requested URL, for example, if the client requests register.do, the controller will send the request to Register.java.
And in Register.java there is a method that accepts the request as a parameter, namely:
public String perform(HttpServletRequest request) {
do something with the request...
}
So the problem is that if I want to print something in UTF-8 inside this method, it will give random characters. For example, I have an Enum that stores several constants, one of the properties that 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());
}
It is correctly printed in Chinese. However, if I put the exact code in a method related to the HttpServletRequest:
public String perform(HttpServletRequest request) {
System.out.println(MyEnum.One.getChn());
logger.info(MyEnum.One.getChn());
}
They print as random characters, but from the debug window (eclipse) you can see that the variables contain the correct Chinese characters.
So the same situation arises when I want to save the value from request.getParameter (). In the debug window, I see that the variable contains the correct characters, but I print it out or try to save it in the database, these are random characters.
, , . - ?
.