Servlet solves Chinese disorder

Source: Internet
Author: User

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
 

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.