About handling client submitting Chinese data garbled problem solving countermeasures __ garbled problem

Source: Internet
Author: User

It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.


One, the servlet handles the client submits the Chinese data garbled problem

Example Program:

for HTML pages, a statement that controls what code the client opens the page in, and what encoding it sends the data to the server:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
for HTML pages, a statement that controls what code the client opens the page in, and what encoding it sends the data to the server:
<!--the value of the Pageencoding property here specifies what encoding the client will use to open this page, and what encoding to send the data to the server--> <%@ page language= "java" import= "java.util.*"
pageencoding= "Utf-8"%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

Processing client three kinds of Chinese data submitted "Chinese" garbled Problem handling mode:

Package edu.request;
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 Encodingservlet extends HttpServlet {public void DoPost (HttpServletRequest request, HttpServletResponse re 
	    Sponse) throws Servletexception, IOException {System.out.println ("--------doPost---------"); /** * Garbled Reason: * 1. The client will encoding.jsp page user input "China" to the encoding.jsp encoding (here is utf-8), the Chinese data in utf-8 encoding * produces binary data sent to the server, such as in the UTF-8 Binary data for "China" in the character set (98,99) * 2.Servlet handler by default encodes binary data (98,99) As string data according to the iso8859-1 character set, because the 98,99 * corresponding character in the iso8859-1 character set is not China, so
		 will be garbled. * Solution: * 1. By specifying the servlet to process the character set of the data in request, the servlet can combine the binary data according to the specified character set, even if the servlet obtains the contents of the parameter according to the * Utf-8 character set reorganization.
		 The method is specified as follows: * request.setcharacterencoding ("Utf-8");
	    **/request.setcharacterencoding ("Utf-8"); String username = request.getparameter ("username");
	SYSTEM.OUT.PRINTLN (username); 
		public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
		System.out.println ("-------doget------"); /** * garbled Reason: * garbled reason and the above to post the Chinese data of the garbled reason for the same * solution: * 1. Due to request.setcharacterencoding ("Utf-8"), only the number submitted on post
		 It is valid that data submitted with get and URL is not valid, * In fact, the URL is also the way to submit data in get. * 2. This can only be used to "break and reorganize" the way to get the correct Chinese data, because the client submitted in Chinese is broken in accordance with UTF-8, to the server, * and Request.getparameter ("username") to obtain data is the servlet according to the default
		 Iso8859-1 character set for reorganization, must be garbled, so should be in accordance with the ISO8859-1 will request.
		 * The data for GetParameter ("username") is broken into bytes, and the "client-entered bytes broken by UTF-8" is reorganized in accordance with Utf-8.
	    * Specific to new string () * **/string username = Request.getparameter ("username");
		Username = new String (username.getbytes ("iso8859-1"), "UTF-8");     	
	SYSTEM.OUT.PRINTLN (username);
 }
	
}
In order from the top down, successively enter "China after", the console output:



Second, the servlet processing server to client response to the Chinese data garbled problem


1. The server-side program writes Chinese data to the client--writes the response object with the byte stream

Byte stream output Chinese garbled problem public class Outputstreamservlet extends HttpServlet {@Override protected void doget (Httpservletreque St req, HttpServletResponse resp) throws Servletexception, IOException {//Specify browser what code table to open server sent data//resp.setheader ("Co          Ntent-type "," text/html;charset=utf-8 ");//code 1//resp.setcontenttype (" Text/html;charset=utf-8 ");
		Code 2 OutputStream out = Resp.getoutputstream ();
	    String data = "China";
	     * * * Garbled Analysis: * Data to Utf-8 code to split into byte data, but the browser by default is in accordance with the GB2312 Code response object data merged into character data, * so there will be garbled. * Solution: * To allow browsers to merge byte data with Utf-8 code, there are four ways * 1. Set header information for response: such as code 1 and code 2 * 2. Write analog header information to the browser, such as code 3: * 3. Click Browser "View"--> "character encoding"--> "UTF-8" Change the character encoding displayed on the page * * * out.write ("<meta http-equiv= ' Content-type '" "content=" text/html;c Harset=utf-8 ' > '. getBytes ());
		
	Code 3 Out.write (Data.getbytes ("Utf-8")); @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, ioexcept Ion {DoGET (req, resp);
 }
}

2. The server-side program writes Chinese data to the client--writes the response object as a character stream

public class Printwriterservlet extends HttpServlet {

//character stream output Chinese garbled problem public
	void Doget (httpservletrequest Request, HttpServletResponse Response)
			throws Servletexception, IOException {
		//Set the code table used by response, To control response to write data to the browser in what Code table.
		response.setcharacterencoding ("Utf-8");
		Specifies what code table the browser will use to open the data sent by the server
        response.setheader ("Content-type", "Text/html;charset=utf-8");
        
        The above two sentences can also be replaced by a word
       //Response.setcontenttype ("Text/html;charset=utf-8");
     
		String data = "China";
		PrintWriter out = Response.getwriter ();
		Out.write (data);
		
	}

	public void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		doget (request, response);
	}
}






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.