Several Chinese Servlet related to the garbage problem

Servlet several Chinese garbage problem related to
browser to call jsp, html page and other Chinese garbled
make the file itself with utf-8 character set edit save

Let the browser browser to parse utf-8 character set

In the browser to select right encoding format is utf-8;

By <meta> tag simulation response header, played coding resolve browser to use the utf-8

To do:

HTML //
<Meta name = "Content-type" Content = "text / HTML; charset = UTF-. 8"> or <Meta charset = "UTF-. 8">
// JSP
<% @ the pageEncoding = "UTF-. 8" %>
// xml
<? xml encoding = "UTF-8"?>
calls the servlet via the browser, page garbled.
response garbage problem

Control browser with UTF-8 is decoded

the response.setContentType ( "text / HTML; charset = UTF-. 8"); 
// or
response.setHeader ( "Content-type", "text / HTML; charset = UTF-. 8");

// PS: is the setHeader HttpServletResponse Methods. To set the character encoding Filter interceptor, the method does not have this, since the parameter type of Filter doFilter method is ServletResponse
the response data object to the UTF-8 byte stream sent to the decoder browser

response.setCharacterEncoding ( "UTF-. 8");
Request garbled

post request garbled

resquest.setCharacterEncoding ( "page using the character set"); // request is valid only for the post
GET request (URL parameter passed by garbled)

the reason:

// The essential problem is the way to get content delivery parameters default encoding ask ISO8859-1, and the use of request.setCharacterEncoding ( "utf-8") can not solve the problem.

Solution one:

Modify the tomcat server's configuration file, which modify conf / server.xml file in the tomcat directory.

<Connector Port = "8080" = Protocol "the HTTP / 1.1"     the maxThreads = "150" connectionTimeout = "200000"     redirecPort = "8443" <-! The URIEncoding = "UTF-. 8" -> /> // need to annotate Add part of solution two:




In the servlet manually convert the character set and converted (not recommended).

// the Servlet corresponding to:
String name = request.getParameter ( "name");
String value = null;
value = new new String (name.getBytes ( "the ISO-8859-1"), "the character set used by the page");
call database garbled
modify the database character set or the character set table

#sql statements
show create table table name; # information look-up tables (mainly to see the character set)
   / * Example: the Table the Create the Table
Dog dog` the CREATE TABLE `(
` dog_id` int (11) the NOT NULL AUTO_INCREMENT,
..... .
a PRIMARY KEY ( `dog_id`)
). 1 = the AUTO_INCREMENT the InnoDB ENGINE = DEFAULT the cHARSET = UTF8 * /
# End modify database character set, need to restart mysql database
ALTER dATABASE database name the sET UTF8 the cHARACTER;
  # modify character set table
ALTER tABLE table name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10962039.html