Servlet中request.getParameter和getParameterValues getParameterNames三者区别

 

getparameter

Servlet中request.getparametergetParameterValuesgetParameterNames三者区别

1.request.getParameter:获取前台表单单个元素name对应的value值

2.request.getparameterValues:获取前台表单多个标签同名name对应的所有value值

3.request.getParameterNames:获取前台表单所有标签元素name的对应的所有value值

例子如下:

先写个allparams.jsp页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getcontextpath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">                   <title>My JSP 'allparams.jsp' starting page      	HTTP-equiv="pragma" content="no-cache"> 	 	expires" content="0">     	words" content="keyword1,keyword2,keyword3"> 	iption" content="This is my page"> 	            
看书 电影 游戏

再写个AllParams.java的servlet程序

 package com.learnservlet.servletexample;  import java.io.IOException;  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;  public class AllParams extends HttpServlet {  	/** 	 * Constructor of the object. 	 */ 	public AllParams() { 		super(); 	}  	/** 	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); System.out.println("1.获取前台表单单个元素name对应的value值"); String submitvalue = request.getParameter("submit"); System.out.println(submitvalue); //相当于获取多个同名复选框的值,放在字符数组中 //相当于 String[] lparamvalues = {value1,value2,value3} System.out.println("2.获取前台表单多个标签同名name对应的所有value值"); String[] paramvalues = request.getParameterValues("habit"); for(String i:paramvalues){ System.out.println(i); } System.out.println("3.获取前台表单所有标签元素name的对应的所有value值"); Enumeration paramNames = request.getParameterNames(); System.out.println(paramNames);//输出枚举对象 while(paramNames.hasMoreElements()){ String paramName = (String)paramNames.nextElement(); String[] paramValue = request.getParameterValues(paramName); for(String j : paramValue){ System.out.println(j); } } } /** * initialization of the servlet.
* * @throws ServletException if an ERROR occurs */ public void init() throws ServletException { // Put your code here } }

Web.xml代码

  APP /jishu/34511.html" target="_blank" class="infotextkey">instance" 	 	xsi_schemaLocation="http:///yunying/37135.html" target="_blank" class="infotextkey">webapp_ID" version="3.1"> 	<display-name>servletlearn 	  	 		AllParams 		com.learnservlet.servletexample.AllParams 	 	 		AllParams 		/AllParams 	  	 	 		index.jsp 	 

打开游览器:地址栏输入http://localhost:8081/servletlearn/AllParams运行

测试结果如下:

相关阅读

发表评论