您的位置:首页 > Web前端 > JavaScript

Hello JSP!——<jsp:param>动作元素篇

2015-09-05 12:27 671 查看
<jsp:param>动作元素用来传递,一般与<jsp:include>、<jsp:forward>联合使用。其语法格式如下。

<jsp:param name="参数名" value="参数值“/>

<jsp:param>动作元素包含两个属性,一个是name,用来设定传递参数的名称;一个是value,用来设定传递参数的值。

一.<jsp:include>动作元素搭配<jsp:param>动作元素

通过<jsp:include>动作元素搭配<jsp:param>动作元素,可以实现在包含文件的同时向被包含文件传递参数。

1.如何使用<jsp:param>动作元素传递一个参数。

示例:ParamInclude.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
	<head>
		<title>包含JSP文件并传递参数</title>
	</head>
	<body>
		使用include动作元素包含一个JSP文件,并传递参数<br>
		<jsp:include page="contentDemo.jsp">
			<jsp:param name="age" value="19" />
		</jsp:include>
	</body>
</html>


contentDemo.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<h2>被包含页</h2>
<p>接收到的参数</p>
<%
	String strAge =request.getParameter("age"); //接受参数
%>
<%--输出参数内容--%>
<%="age参数值为" + strAge %>


运行结果:





2.如何通过<jsp:param>动作元素来传递多个参数。

示例:ParamInclude2.jsp

<%@ page language="java" contentType="text/html;charset=gb2312" %>
<html>
	<head>
		<title>包含JSP文件并传递多个参数</title>
	</head>
	<body>
		使用include动作元素包含一个包含JSP文件,并传递多个参数<br>
			<jsp:include page="contentDemo2.jsp">
				<jsp:param name="name" value="Jame" />
				<jsp:param name="age"  value="19"   />
				<jsp:param name="sex"  value="man"  />
		</jsp:include>
	</body>
</html>

contentDemo2.jsp

<%@ page language="java" contentType="text/html;charset=gb2312" %>
<h2>被包含页</h2>
<p>接收到的参数</p>
<%
	String strName = request.getParameter("name");
	String strAge  = request.getParameter("age");
	String strSex  = request.getParameter("sex");
%>
<%-- 输出参数内容 --%>
<%="name参数值为" + strName %>
<%=",age参数值为" + strAge  %>
<%=",sex参数值为" + strSex  %>


运行结果:





二.<jsp:forward>动作元素搭配<jsp:param>动作元素

通过<jsp:forward>动作元素搭配<jsp:param>动作元素,可以实现在跳转页面的同时向跳转页面传递参数。

使用<param>动作元素传递一个参数。

示例:ParamForward.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
	<head>
		<title>跳转并传递参数</title>
	</head>
	<body>
		使用forward动作元素跳转到另一个JSP文件,并传递参数<br>
		<jsp:forward page="ForwardedDemo.jsp" >
			<jsp:param name="age" value="19" />
		</jsp:forward>
	</body>
</html>


ForwardedDemo.jsp

<%@ page language="java" contentType="text/html;charset=gb2312" %>
<html>
	<head>
		<title>跳转到的页面</title>
	</head>
	<body>
		<h2>跳转到的页面</h2>
		<p>接收到的参数</p>
		<%
			String strAge = request.getParameter("age");   //接收参数
		 %>
		 <%-- 输出参数内容 --%>
		 <%="age参数值为" + strAge %>
	</body>
</html>


运行结果:





多个<jsp:param>动作元素传递多个参数。

示例:ParamForward2.jsp

<%@ page language="java" contentType="text/html;charset=gb2312" %>
<html>
	<head>
		<title>跳转并传递参数</title>
	</head>
	<body>
		使用forward动作元素跳转到另一个JSP文本,并传递参数。<br>
		<jsp:forward page="ForwardedDemo2.jsp">
			<jsp:param name="name" value="Jame"/>
			<jsp:param name="age"  value="19"  />
			<jsp:param name="sex"  value="man" />
		</jsp:forward>
	</body>
</html>


ForwardedDemo2.jsp

<%@ page language="java" contentType="text/html;charset=gb2312" %>
<html>
	<head>
		<title>跳转到的页面</title>
	</head>
	<body>
		<h2>跳转到的页面</h2>
		<p>接收到的参数</p>
		<%
			String strName = request.getParameter("name");  //接收参数name
 			String strAge  = request.getParameter("age");   //接收参数age
			String strSex  = request.getParameter("sex");   //接收参数sex
		 %>
		 <%-- 输出参数内容 --%>
		 <%="name参数值为" + strName %>
		 <%="age参数值为"  + strAge %>
		 <%="sex参数值为"  + strSex %>
	</body>
</html>


运行结果:







通过使用<jsp:param>动作元素来为包含(跳转)页面传递多个参数。被包含的JSP文件通过使用request.getParameter()方法,来接受这些参数值,并赋值给字符串变量,最后通过JSP表达式输出变量的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: