request.getParameterNames_request和urllib区别

request.getParameterNames_request和urllib区别Servlet中request.getParameter和getParameterValues getParameterNames三者区别1.request.getParameter:获取前台表单单个元素name对应的value值2.request.getParameterValues:获取前台表单多个标签同名name对应的所有value值3.request.getParameterN…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Servlet中request.getParameter和getParameterValues 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运行

request.getParameterNames_request和urllib区别

测试结果如下:

request.getParameterNames_request和urllib区别

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197472.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python的for循环_python中的while语句

    Python的for循环_python中的while语句for循环和while循环有什么区别?众做周知,循环是Python中最基础也是最常见的知识点之一,下面我们来一起好好学习一下for循环和while循环,并对比分析两者的使用区别,帮助Python初学者可以更好地掌握两者的内容和应用,下面一起来看看吧~1、for循环(1)概念for循环可以遍历任何序列的项目,比如字符串、列表、元组、字典、集合等序列类型,逐个获取序列中的各个元素。(2)代码示例:终端…

    2022年8月12日
  • vue笔记(2)vue中使用ajax

    vue笔记(1):https://blog.csdn.net/qq_43537987/article/details/93402865Vue中利用vue-resource发起Ajax请求除了vue-resource之外,还可以使用axios的第三方包实现实现数据的请求参考网址:https://www.runoob.com/vue2/vuejs-ajax.htmlhttp…

    2022年4月9日
  • mysql insert sql

    mysql insert sql

    2022年3月4日
  • Dubbo入门_dubbo的原理

    Dubbo入门_dubbo的原理dubbo分布式系统简介发展演变RPCdubbo核心概念搭建dubbo分布式系统简介“分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统”分布式系统(distributed system)是建立在网络之上的软件系统。随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。发展演变单一应用架构当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时

    2022年8月8日
  • 信道和带宽_信道带宽怎么计算

    信道和带宽_信道带宽怎么计算信道和带宽在用cmw500测试不同band下的throughput时,发现module在某几个band注册不上小区。后来经过同事顺滑的演示,得知是因为不同band支持不同的带宽,而我一直设置cmw500的Cellbandwidth=20MHZ,对于那些最大只支持10MHZ的band自然注册不上。关于不同Band支持的带宽可以参考下表(3GPPTS36.101V17.2.0(2021-06))Table5.6.1-1:E-UTRAchannelbandwidth结尾处分享一

    2022年10月7日
  • SQL中EXISTS的用法

    SQL中EXISTS的用法比如在Northwind数据库中有一个查询为SELECTc.CustomerId,CompanyNameFROMCustomerscWHEREEXISTS(SELECTOrderIDFR

    2022年6月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号