GET the data submitted by GET/POST and handle Chinese problems

Source: Internet
Author: User

1. Obtain the value in the input tag, and use request. getParameter ("User") (User is the name value of input)

2. Get the checkbox value. Because multiple values are selected, you cannot use getParameter. Because getParameter can only get one value, you can use string [] str [request. getParameterValues ("love") to obtain the array, and then use Arrays. toString (str) gets the value selected for checkbox;

3. Processing of submitted Chinese content

When the content submitted in the Get method contains Chinese characters: (only one data can be processed)

 

1 String say = request. getParameter ("say"); // get the value say = % E4 % E82 String str = new String (say. getBytes ("ISO-8859-1"), "UTF-8"); // transcoding, http transport uses ISO-8859-1 encoding, we want to convert to UTF-8 encoding 3 System. out. println (str );

 

 

 

When the content submitted in post mode contains Chinese characters:

Set the encoding directly before obtaining the code (set all the codes once and for all [recommended ])

Request. setCharacterEncoding ("UTF-8 ");

 

Demo

1 System. out. println ("Get"); 2 System. out. println (request. getParameter ("User"); 3 System. out. println (request. getParameter ("Pwd"); 4 System. out. println (request. getParameter ("sex"); 5 System. out. println (Arrays. toString (request. getParameterValues ("love"); 6 7 String say = request. getParameter ("say"); 8 String str = new String (say. getBytes ("ISO-8859-1"), "UTF-8"); 9 System. out. println (str );
1 System. out. println ("post"); 2 3 request. setCharacterEncoding ("UTF-8"); 4 5 System. out. println (request. getParameter ("User"); 6 System. out. println (request. getParameter ("Pwd"); 7 System. out. println (request. getParameter ("sex"); 8 System. out. println (Arrays. toString (request. getParameterValues ("love"); 9 System. out. println (request. getParameter ("say "));

 

1 <form action = "getDataServlet" method = "get"> 2 Username: <input type = "text" name = "User"/> 3 <br/> 4 password: <input type = "password" name = "Pwd"/> 5 <br/> 6 gender: <input type = "radio" name = "sex" checked = "checked" value = "01"/> male 7 <input type = "radio" name = "sex" value = "02"/> Female <br/> 8 Hobbies: <input type = "checkbox" name = "love" value = "o1"/> reading 9 <input type = "checkbox" name = "love" value = "o2"/> swimming 10 <input type = "checkbox" name = "love" value = "o3"/> hitting game 11 <input type = "checkbox" name = "love" value = "o4" /> programming 12 <input type = "checkbox" name = "love" value = "o5"/> Watch Movies 13 <br/> 14 self-evaluation: <textarea rows = "5" cols = "30" name = "say"> </textarea> 15 <br/> 16 <input type = "submit" value = "submit" /> 17 </form>

 

GET the data submitted by GET/POST and handle Chinese problems

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.