JSP Chinese garbled Processing

Source: Internet
Author: User
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.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.