1. Solve the Problem of Chinese disorderly editing
Requirement 1: write a program to test the Chinese garbled characters caused by passing form parameters, and solve the problem of Chinese garbled characters.
(1) What should I do when the submission method is POST?
(2) What should I do when the submission method is GET?
Requirement 2: Compile the Servlet program and use PrintWriter to output Chinese characters on the Servlet page to solve this problem.
[Html] <% @ page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
</Head>
<Body>
<Center>
<H1>
Solve Chinese disorder
</H1>
<Form action = "check" method = "get">
<Table border = "1">
<Tr>
<Td colspan = "2">
<H2>
Get submission method
</H2>
</Td>
</Tr>
<Tr>
<Td>
Text:
</Td>
<Td>
<Input type = "text" name = "username">
</Td>
</Tr>
<Tr>
<Td colspan = "2">
<Input type = "submit" value = "submit">
</Td>
</Tr>
</Table>
</Form>
<Form action = "check" method = "post">
<Table border = "1">
<Tr>
<Td colspan = "2">
<H2>
Post submission method
</H2>
</Td>
</Tr>
<Tr>
<Td>
Text:
</Td>
<Td>
<Input type = "text" name = "username">
</Td>
</Tr>
<Tr>
<Td colspan = "2">
<Input type = "submit" value = "submit">
</Td>
</Tr>
</Table>
</Form>
</Center>
<Pre>
When passing Chinese characters as parameters, Chinese garbled characters are easily generated. You can solve the problem by using the following methods:
POST transfer: request. setCharacterEncoding ("GBK ");
GET transfer:
Transcode the received parameters.
String name = request. getParameter ("username ");
Name = new String (name. getBytes ("ISO8859-1"), "GBK ");
Modify server. xml
URIEncoding = "GBK"
</Pre>
</Body>
</Html>
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
</Head>
<Body>
<Center>
<H1>
Solve Chinese disorder
</H1>
<Form action = "check" method = "get">
<Table border = "1">
<Tr>
<Td colspan = "2">
<H2>
Get submission method
</H2>
</Td>
</Tr>
<Tr>
<Td>
Text:
</Td>
<Td>
<Input type = "text" name = "username">
</Td>
</Tr>
<Tr>
<Td colspan = "2">
<Input type = "submit" value = "submit">
</Td>
</Tr>
</Table>
</Form>
<Form action = "check" method = "post">
<Table border = "1">
<Tr>
<Td colspan = "2">
<H2>
Post submission method
</H2>
</Td>
</Tr>
<Tr>
<Td>
Text:
</Td>
<Td>
<Input type = "text" name = "username">
</Td>
</Tr>
<Tr>
<Td colspan = "2">
<Input type = "submit" value = "submit">
</Td>
</Tr>
</Table>
</Form>
</Center>
<Pre>
When passing Chinese characters as parameters, Chinese garbled characters are easily generated. You can solve the problem by using the following methods:
POST transfer: request. setCharacterEncoding ("GBK ");
GET transfer:
Transcode the received parameters.
String name = request. getParameter ("username ");
Name = new String (name. getBytes ("ISO8859-1"), "GBK ");
Modify server. xml
URIEncoding = "GBK"
</Pre>
</Body>
</Html>
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-name> encoding </servlet-name>
<Servlet-class> com. mars. check </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> encoding </servlet-name>
<Url-pattern>/check </url-pattern>
</Servlet-mapping>
</Web-app>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-name> encoding </servlet-name>
<Servlet-class> com. mars. check </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> encoding </servlet-name>
<Url-pattern>/check </url-pattern>
</Servlet-mapping>
</Web-app>
[Java]
Package com. mars;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class check extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = GBK ");
String newname = new String (request. getParameter ("username"). getBytes (
ISO-8859-1), GBK ");
PrintWriter out = response. getWriter ();
Out. print ("Out. print ("Out. close ();
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = GBK ");
// Request. setCharacterEncoding ("GBK ");
System. out. println ("Call doPost ");
String newname = request. getParameter ("username ");
PrintWriter out = response. getWriter ();
Out. print ("Out. print ("Out. close ();
}
}
Package com. mars;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class check extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = GBK ");
String newname = new String (request. getParameter ("username"). getBytes (
ISO-8859-1), GBK ");
PrintWriter out = response. getWriter ();
Out. print ("Out. print ("Out. close ();
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = GBK ");
// Request. setCharacterEncoding ("GBK ");
System. out. println ("Call doPost ");
String newname = request. getParameter ("username ");
PrintWriter out = response. getWriter ();
Out. print ("Out. print ("Out. close ();
}
}
From learning IT from Mars