Common examples of Chinese garbled JSP code and Solutions

Source: Internet
Author: User

In JSP development, Chinese Garbled text is a common problem. Its root cause is: Web containers.DefaultThe character processing encoding of is a ISO-8859-1.

Instance 1: When the JSP page is displayed

<HTML> 

Running result:

 

Solution:Specify a Chinese character set for it, before <HTML>

<%@ page contentType="text/html;charset=gb2312" %>

Example 2: When the JSP page passes Chinese Parameters

Registration page:

<% @ Page contenttype = "text/html; charset = gb2312 "%> <HTML> 

Personal information page:

<% @ Page contenttype = "text/html; charset = gb2312 "%> <HTML> 

Running result:

Solution:ModifyPersonal Information PageAs follows:

<% @ Page contenttype = "text/html; charset = gb2312 "%> <HTML> 

Example 3: When the servlet processes Chinese Parameters

Registration page:

<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page import = "test. usermsg "%> <HTML> 

Usermsg. Java (servlet)

Package test; importjava. io. ioexception; importjava. io. printwriter; importjava. io. unsupportedencodingexception; importjavax. servlet. HTTP. httpservlet; importjavax. servlet. HTTP. httpservletrequest; importjavax. servlet. HTTP. httpservletresponse; Public classusermsg extends httpservlet {public void doget (response, httpservletresponse response) {dopost (request, response);} public void dopost (response, httpservletresponse response) {try {request. setcharacterencoding ("gb2312");} catch (unsupportedencodingeffectione) {e. printstacktrace ();} printwriter out = NULL; try {out = response. getwriter ();} catch (ioexception E1) {e1.printstacktrace ();} Out. print ("<HTML>"); out. print ("<body>"); out. print ("<H2>" + "user information:" + "</H2>"); out. print ("<p>" + "Email:" + request. getparameter ("email") + "<p/>"); out. print ("<p>" + "nickname:" + request. getparameter ("nickname") + "<p/>"); out. print ("<p>" + "Password:" + request. getparameter ("password") + "<p/>"); out. print ("<p>" + "Gender:" + request. getparameter ("sex") + "<p/>"); out. print ("<p>" + "personal introduction:" + request. getparameter ("Introduction") + "<p/>"); out. print ("

Running result:


Solution:Add the following to dopost:

response.setContentType("text/html; charset=gb2312");

 

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.