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");