First use a user submission interface as an example (text box, password box, selection, drop-down form, etc.), the effect is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>User Registration</title></head><body><!-- User Registration--><form action="/requesttest/request5" method="get"><table><!-- Text input box--><tr><td>Username</td><td><input type="text" name="username"/></td></tr><!-- Password box--><tr><td>Password</td><td><input type="password" name="password" /></td></tr><!-- Radio button radio--><tr><td>Gender</td><td><input type="radio" name="gender" value="male" /> Male <input type="radio" name="gender" value="female" /> Female</td></tr><!-- Check box--><tr><td>Hosts</td><td><input type="checkbox" name="hobby" value="sport" /> Sports<input type="checkbox" name="hobby" value="music" /> Music<input type="checkbox" name="hobby" value="game" /> Games</td></tr><!-- Pull-down box--><tr><td>City</td><select name="city"><option value="beijing">Beijing</option><option value="shanghai">Shanghai</option><option value="shenzhen">Shenzhen</option></select></td><!-- Multi-line text box--><tr><td>Personal Profile</td><td><textarea rows="5" cols="60" name="introduce"></textarea></td></tr><tr><td colspan="2"><input type="submit" value="Register"/></td></tr></table></form></body></html>
Note: The definition and usage of the HTML < form> tag are:
<!--Required action attribute specifies where to send form data when submitting a form. --><form action="value">
The attribute value is a URL, indicating where to send form data. Its possible values:
Absolute URL - Point to other sites (such as src=”www.example.com/example.htm”)
Relative URL - Point to files within the site (such as src=”example.htm”)
For example, the following form has two input fields and a submit button. When submitting the form, the form data is submitted to a page named "form_action.asp":
<form action="form_action.asp" method="get"><p>First name: <input type="text" name="fname" /></p><p>Last name: <input type="text" name="lname" /></p><input type="submit" value="Submit" /></form>
The method is get, so the information is retrieved in the doGet method of the servlet
public class RequestServlet5 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// Solve post garbled code// request.setCharacterEncoding("utf-8");// Get specified data through getParameter String username = request.getParameter("username");System.out.println(username); // Get a value// Solve get garbled code (for example, enter Chinese) --- Use manual encoding// username = URLEncoder.encode(username, "ISO-8859-1");// Encoding with ISO// username = URLDecoder.decode(username, "utf-8"); // Decoding with utf-8 username = new String(username.getBytes("ISO-8859-1"), "utf-8"); System.out.println(username);// Non-null verification if (username != null && username.trim().length() > 0) {System.out.println("username legal");}// Use getParameter to obtain checkbox (checkbox) to submit data. By default, only the first data can be obtained. String hobby = request.getParameter("hobby"); // Multiple-check box System.out.println(hobby);// Get all submitted data for checkbox --- getParameterValuesString[] hobbies = request.getParameterValues("hobby");System.out.println(Arrays.toString(hobbies));System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- request.getParameterNames(); while (names.hasMoreElements()) {String name = names.nextElement();// Get each parameter name System.out.println(name + ":"+ Arrays.toString(request.getParameterValues(name)));}System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ parameterMap.keySet();for (String key : keys) { // key is the parameter nameSystem.out.println(key + ":"+ Arrays.toString(parameterMap.get(key)));}} public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}Reasons for garbled parameters
URL encoding is a format used by browsers to package form input. The browser gets all names and values from the form, encodes them in name/value parameters (removes those characters that cannot be transferred, ranks data, etc.) as part of the URL or separately sends them to the server.
Different request methods correspond to different solutions:
post ―- request.setCharacterEncoding("Client encoding set");
Get garbled code solves manually
username = URLEncoder.encode(username, "ISO-8859-1");// Encode username = URLDecoder.decode(username, "utf-8"); // Decode with utf-8
Simplify the above writing method: username = new String(username.getBytes("ISO-8859-1"), "utf-8");
Get garbled configuration tomcat default decoded character set
in tomcat/conf/server.xml
Add a property URIEncoding="utf-8" in the Connector
Conclusion: During development, try not to modify the default decoding set of tomcat. Please try to use post as much as possible to submit a request. If you have to use get, manually encode it.