在Web應用中,對於客戶端數據的收集一般會使用表單來進行。當用戶端請求到達服務器時,Servlet主要經過HttpServletRequest對象的三種方法來獲取用戶請求的參數:javascript
(1)Public String getParameter(String name):用來獲取表單單個請求參數的值。html
(2)Public Enumeration getParameterNames():若是您想要獲得當前請求中的全部參數的完整列表,則調用該方法。java
(3)Public String[] getParameterValues(String name):若是參數name的值不止一個,則調用該方法,並返回多個值。服務器
示例以下:post
(1)源代碼this
submitForm.htmlspa
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Submit From</title> </head> <body> <form action="showParameter" method="get" id="submitFrom"> <label for="method">提交方式</label> <select id="method"> <option value="get" name="submitmethod" selected="selected">get</option> <option value="post" name="submitmethod">post</option> </select> <br/> <script type="text/javascript"> document.getElementById("method").onchange = function(){ var index = this.selectedIndex; var selMethod = this.options[index].value; document.getElementById("submitFrom").method = selMethod; } </script> <label for="man">男</label> <input type="checkbox" value="man" id="sex" name="sex"> <label for="woman">女</label> <input type="checkbox" value="woman" id="woman" name="sex"><br> <label for="name">姓名</label> <input type="text" id="name" name="name"><br> <label for="age">年齡</label> <input type="text" id="age" name="age"><br> <input type="submit" name="submit"> </form> </body> </html>
showParameter.javacode
package cn.border.parameter; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class showParameter */ @WebServlet("/showParameter") public class showParameter extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public showParameter() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //解決中文參數亂碼 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = (String) parameterNames.nextElement(); out.println(parameterName+" : "); String[] parameterValues = request.getParameterValues(parameterName); if (parameterValues.length == 1) { //只有一個值 String parameterValue = parameterValues[0]; if(parameterValue.length() == 0){ out.println("值爲空"); }else { out.print(parameterValue); } } else { // for (String parametervalue : parameterValues) { out.println(parametervalue+" | "); } } out.println("<br>"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
(2)運行結果orm