HTTP protocol 4---Get way and post way to obtain form data Example 3 (Unified way to get, solve Chinese garbled problem) __ garbled problem

Source: Internet
Author: User

HTTP protocol---Get way and post way to obtain form data Example 3 (Unified way to get, solve Chinese garbled problem)

The previous article only applies to the submitted data are in English, but if it is in Chinese will appear garbled situation, because we specify the encoding format for UTF-8, but the server default encoding format for iso-8859-1, so to solve the problem of garbled code conversion needs to be

Note: Post mode parameter encoding: request.setcharacterencoding ("Utf-8");

Get mode parameter encoding: Manual single decode---> String name = new String (name.getbytes ("iso-8859-1"), "Utf-8");

HTML file: testparam.html

<! DOCTYPE html>  

Main program Demo_request2.java

Package Request;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.PrintWriter;

Import java.util.Enumeration;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; * * Gets and post-submit parameters */public class Demo_request2 extends HttpServlet {public void doget (HttpServletRequest requ EST, httpservletresponse response) throws Servletexception, IOException {* * * Set the code of the parameter query to solve the situation of the Chinese garbled by different coding parsing (directly change the whole , convenient) * But this method only works on the data encoding that requests the entity content.
		 Post-submitted data is in the entity content, * So this method works on the Post method, and the Get method's arguments are placed behind the URI, so they are not valid for Get methods ...
		 * So the Get method can only be decoded manually ...
		* * request.setcharacterencoding ("Utf-8");
		* * Unified and convenient access to request parameter method * * SYSTEM.OUT.PRINTLN (Request.getmethod () + "way to obtain data");
		Gets the parameter value based on the parameter name (only one worthy parameter) String name = Request.getparameter ("name"); * * Manual re-decoding (Ios-8859-1 string-> utf-8 string) (only a single decoding format, more cumbersome) * But the Get method can only be manually decoded/if ("Get". Equals (Request.getmethOD ())) {name = new String (name.getbytes ("iso-8859-1"), "Utf-8");
		String Password = request.getparameter ("password");
		if ("Get". Equals (Request.getmethod ()) {password = new String (password.getbytes ("iso-8859-1"), "Utf-8");
		} System.out.println (name+ "---" +password);
		System.out.println ("=================================================");
		Gets all the parameter names and values enumeration<string> enums = Request.getparameternames ();
			while (Enums.hasmoreelements ()) {String paramname = enums.nextelement (); if ("Hobit". Equals (ParamName)) {/* * * * Gets the parameter value based on the parameter name (but can get multiple values)/string[] Hobits = Request.getparametervalu
				ES ("Hobit"); for (String h:hobits) {if (' Get '. Equals (Request.getmethod ())) {h = new String (h.getbytes ("iso-8859-1"), "Utf-8
					");
				} System.out.println (paramname+ "---" +h+ ",");
				} else {String paramvalue = Request.getparameter (paramname); if ("Get". Equals (Request.getmethod ()) {paramvalue = new String (paramvaLue.getbytes ("Iso-8859-1"), "Utf-8");
			} System.out.println (paramname+ "---" +paramvalue); }} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexce ption {//System.out.println ("POST method Submission");//InputStream in = Request.getinputstream ();//byte[] buf = new byte[1024]
;
int len = 0; while (len = In.read (buf))!=-1) {//System.out.println (new String (Buf,0,len));//}//System.out.println (re
Quest.getmethod () + "way to obtain data");
Gets the parameter value//String name = Request.getparameter ("name") according to the parameter name;
String Password = request.getparameter ("password");
System.out.println (name+ "---" +password);
Gets all the parameter names and parameter values//enumeration<string> enums = Request.getparameternames (); while (Enums.hasmoreelements ()) {//String paramname = Enums.nextelement ();//String paramvalue = Request.getpara
meter (paramname);
System.out.println (paramname+ "---" +paramvalue); }//Must call the Doget method This.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.