Subject: |
|
Share my JSP Chinese character processing experience (original) by the way) |
Author: |
|
Joyous (null) |
Level 1: |
|
|
Reputation value: |
|
99 |
Community: |
|
Java Web Development |
Problem points: |
|
100 |
Replies: |
|
1 |
Posting time: |
|
09:38:12 |
Java character encoding settings in JSP code
<% @ Page contenttype = "text/html" %>
<% @ Page pageencoding = "UTF-8" %>
<% @ Page import = "java.net. urlencoder" %>
(It seems that the previous statement is not necessary)
Red for the preparation of JSP processing characters using UTF-8 encoding.
<% Request. setcharacterencoding ("UTF-8"); %>
Java code in JSP sets the receiving parameter to UTF-8 encoding, in Form submission, post or get support Chinese, if not set, you can only use the get method.
Encoding of HTML headers in JSP
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> note page </title>
</Head>
<Body>
......
......
The red part UTF-8 makes the encoding method used for HTML pages
The form submission code is as follows:
<Form action = index. jsp method = "Post"> <Input type = "text" size = "30" name = "yourname" value = "">
<Input type = submit value = "Submit">
</Form>
The receiving code is as follows:
<%
String temp1 = request. getparameter ("yourname ");
......
%>
Hyperlink parameters are transmitted in a slightly different way. If you only use the above settings, If you directly submit Chinese parameters, some text will get the parameters, and you must encode them before submitting them.
<A href ="
<% = Request. getcontextpath () %>/index. jsp? Yourname =
<% = Java.net. urlencoder. encode (Joho, "UTF-8") %> ">
<% = Myname %>
</A>
By java.net. urlencoder. the variables to be sent by the encode function are parsed to hexadecimal numbers for URL transmission. If the received information is not encoded, it is probably/ufffd or a defective Chinese character. The example receiving code is as follows:
<% String STR = request. getparameter ("passed parameter name"); %>
<P> Hello: <% = STR %> </P>
As a result, the STR after obtaining the parameter will be the correct Chinese information.
After setting will perfectly solve the problem of JSP Chinese character parameters, MySQL is also set to UTF-8 encoding, database access is no problem, Tomcat server environment for any settings.