This article describes the use of various methods of request object in Java. Share it for your reference, as follows:
The request object is to issue a request from the client to the server, including the information submitted by the user and some information from the client. The request object is an implementation instance of the javax.servlet.http.HttpServletRequest class.
The request object encapsulates the browser's request information, and various request information submitted by the client and the user can be obtained through various methods of the request object.
Common methods for using the request object to obtain request parameters submitted by the client are as follows:
1.String getParameter(String name), gets the client's parameter value, and returns the value of the specified parameter in the form of a string . If the parameter does not exist, it returns a null value. Use this method when passing parameters with a form, link, or URL bar.
For example, get the parameter value of the client name:
The code copy is as follows: String name = request.getParameter("name");
2.String[ ] getParameterValues(String name), get all parameter values of a single parameter, mainly used to get the value of the check box , the return value type is the string array String[ ]
For example, get all values of the client hobby check box:
String[ ] hobbys = request.getParameterValues("hobby");if(hobbys != null){out.println("Your hobby is:");for(int i=0;i<hobbys.length;i++) out.println(hobbys[i]);}3. void setCharacterEncoding(String encoding), sets character encoding method to solve the garbled problem of passing non-English characters .
For example, copy the code code as follows: request.setCharacterEncoding("UTF-8");
Example: Use the request object to implement user registration function
The source code of zhuce.html is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Personal Information Registration</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h1 align="center">Personal information registration</h1> <form action="zhuce.jsp" method="post"> Name: <input type="text" name="name"><br> Password: <input type="password" name="pwd"><br> Please select your career: <input type="radio" name="career" value="farmer">Farmer<input type="radio" name="career" value="worker">Worker<input type="radio" name="career" value="student" checked>Student<input type="radio" name="career" value="teacher">Teacher<br> Your favorite city: <select name="city"> <option value="Liaoning Province">Liaoning Province</option> <option value="Hubei Province">Hubei Province</option> <option value="Henan Province">Henan Province</option> <option value="Shandong Province">Shandong Province</option> <option value="Jiangsu Province">Jiangsu Province</option> <option value="Hunan Province" selected>Hunan Province</option> </select> <br> Please select your hobby: <input type="checkbox" name="hobby" value="Tourism">Tourism<input type="checkbox" name="hobby" value="reading book" checked>reading book<input type="checkbox" name="hobby" value="game">game<input type="chess" name="hobby" value="qin, chess, calligraphy and painting">qin, chess, calligraphy and painting<br> Self-introduction: <textarea name="intro">self-introduction</textarea> <br> <input type="submit" name="submit" value="submit"> </form> </body></html>
The source code of zhuce.jsp is as follows:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=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"><html> <head> <base href="<%=basePath%>"> <title>Personal Information Registration</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%request.setCharacterEncoding("UTF-8"); %> Your name is: <%=request.getParameter("name") %><br> Your password is: <%=request.getParameter("pwd") %><br> Your occupation is: <%=request.getParameter("career") %><br> Your favorite city is: <%=request.getParameter("city") %><br> Your hobbies are: <%String[] hobbys = request.getParameterValues("hobby"); if(hobbys != null) { out.println("Your hobbies are:"); for(int i=0;i<hobbys.length;i++) out.print(hobbys[i]); } %> <br> Self-introduction: <%=request.getParameter("intro") %><br> </body></html>I hope this article will be helpful to everyone's Java programming.