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

Servlet中request.getParametergetParameterValues getParameterNames三者区别

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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'allparams.jsp' starting page</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>
 <form action="AllParams" method="POST" target="_blank">
	 <input type="checkbox" name="habit" value="read">看书
	 <input type="checkbox" name="habit" value="movie">电影
	 <input type="checkbox" name="habit" value="game">游戏
	 <input type="submit" name="submit" value="提交">
 </form>
  </body>
</html>

再写个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. <br>
	 */
	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. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

Web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>servletlearn</display-name>
	

	<servlet>
		<servlet-name>AllParams</servlet-name>
		<servlet-class>com.learnservlet.servletexample.AllParams</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>AllParams</servlet-name>
		<url-pattern>/AllParams</url-pattern>
	</servlet-mapping>

	<!-- 默认界面 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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

测试结果如下:

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值