Using UTF-8 is actually a more convenient encoding for JSP because web pages often need to input or output multiple languages.
The JSP transcoding setting is actually not difficult. Just remember to add it. Otherwise, the default encoding of JSP is not UTF-8.
Unfortunately, MySQL does not yet fully support UTF-8.
But don’t be discouraged because we can access UTF-8 encoded data through the JDBC interface of Connection/J provided by MySQL. Using JDBC to automatically transcode is very convenient for those of us who write web pages
!! Without further ado, let’s get to the topic. I will explain it in a few steps below. There are very few, but they are all places that need to be set. I just put them forward for everyone’s attention.
1. Connect to MySQL through JDBC.
12
Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://[hostname]/[database]?user=&password=&useUnicode=true&characterEncoding=utf -8");
The red text must be added because it is a call (it should be OK to call). JDBC uses UTF-8 encoding to access data. To put it simply, JSP tells JDBC... I only want UTF-8. I won’t talk about anything else. ...
Let’s understand it this way^^"
PS:
In fact, I recommend at least using JSP include for this part. It is less error-prone. Just include it when needed. If there is a problem, just look for a file. Don't have to check for errors every time like me...
The previous problem was that there was a file whose JDBC encoding was set to Big5.
It took me a long time to find it...
For more advanced friends, it should be easier to try using JavaBean to control
2.
The part of the web page must be set to the following code
1
<%@ page contentType="text/html;charset=utf-8" %>
It tells JSP and browsers (such as IE) that my encoding should be displayed
in UTF-8. The pageEncoding below can be added or not... I am used to adding it. I don't know if there will be any problems. Please refer to javmon65's great insight. The article will help you learn more http://www.javaworld.com.tw/jute/post/view?bid=6&id=27113&sty=2
1
<%@ page pageEncoding="utf-8" %>
3.
When you use Form to transfer data to another web page, the encoding mode for receiving data must also be set. Otherwise, if you use the JSP default encoding, all the encoding you see will be garbled.
1
<% request.setCharacterEncoding("utf-8"); %>
4.
On the contrary. If you pass the information out (the form seems to be included. I am not sure about this, I have to ask you for help)
You must set the following code
1
<% response.setContentType("text/html;charset=utf-8"); %>
Tell Tomcat that UTF-8 needs to be used to handle data transfer
5.
This is also a very important point. Remember to save it in UTF-8 format after editing your JSP file because I use Dreamweaver MX.
MX will automatically save files for you based on ContentType, so don’t worry. But if you use NotePad to write JSP, you have to select UTF-8 format to save in the file format. Otherwise, the web page will be displayed as garbled characters.
ex: You save it in Big5 format. But the web page encoding is set to UTF-8.
When JSP communicates with the browser, it is naturally processed in UTF-8, so the Big5 format will be converted to UTF-8... Of course it will be garbled, so remember this is a very important thing