jsp调用getParameterValues获取表单信息

2022-09-15 16:26:42 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

1.新建一个Dynamic Web Project项目,里面新建两个jsp文件,整体的结构如下所示:

2.one.jsp文件里面的代码如下所示:

代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example</title>
</head>
<body>
	<form action="two.jsp">
		选择你去过的城市:<br>
		<input type="checkbox" name="cities" value="beijing"/>北京
		<input type="checkbox" name="cities" value="shanghai"/>上海
		<input type="checkbox" name="cities" value="xianggang"/>香港
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

3.two.jsp文件里面的代码如下所示:

代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example</title>
</head>
<body>
	<%
		String yourCities[] = request.getParameterValues("cities");
		for (int i = 0; i < yourCities.length; i  ) {
			out.print(yourCities[i]   "<br>");
		}
	%>
</body>
</html>

4.在浏览器里面输入http://localhost:8080/meme/one.jsp运行之后得到的结果如下所示:

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

0 人点赞