jsp中forward动作与param动作

【转载内容】forward动作是指页面跳转至某一页面,而param动作常常和forward动作一起使用,作为其子标签,为页面跳转添加一些参数。
1、forward动作的语法:<jsp:forward page="URL"/>,其中page是指跳转的页面名,forward动作类似于服务器内部转发方法:request.getRequestDispatcher("/url").forward(request, response)
2、param动作的语法:<jsp:param value="参数值" name="参数名"/>,使用param不仅可以新增一个参数,也可以修改原有的参数值。【转载结束】

下面为跟着视频敲的几个例子:

1.首先创建一个登陆页面(login.jsp),其中form表单提交给forward_action.jsp处理

<%@ 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>用户登陆界面</title>
</head>
<body>
	<form name="loginForm" action="forward_action.jsp" method="post">
		<table>
			<tr>
				<td>UserName:</td>
				<td><input type="text"  name="username"/></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" name="password"/></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="登陆"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

2.创建forward_action.jsp,使其跳转到user.jsp(用户资料界面)

<%@ 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>forward指令 进行跳转</title>
</head>
<body>
	<h1>forward动作</h1>
	<jsp:forward page="user.jsp" />
	<!-- 
		<% request.getRequestDispatcher("user.jsp").forward(request, response); %>
	 -->
</body>
</html>

注意:jsp:forward 动作等价于request.getRequestDispatcher("跳转页面名称").forward(request, response); 

【by the way】getRequestDispatcher是服务器内部跳转,地址栏信息不变,只能跳转到web应用内的网页。 
sendRedirect是页面重定向,地址栏信息改变,可以跳转到任意网页。

3.user.jsp代码

<%@ 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>登陆成功</title>
</head>
<body>
	<h1>用户资料</h1>
    <hr>
    <% 
       request.setCharacterEncoding("utf-8");
       String username = "";
       String password = "";
       if(request.getParameter("username")!=null)
       {
          username = request.getParameter("username");
       }
       if(request.getParameter("password")!=null)
       {
          password = request.getParameter("password");
       }
    %>
        用户名:<%=username %><br>
        密码:<%=password %><br>
</body>
</html>

【增加param标签】

1.login.jsp中表单提交到dologin.jsp

<%@ 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>用户登陆界面</title>
</head>
<body>
	<form name="loginForm" action="dologin.jsp" method="post">
		<table>
			<tr>
				<td>UserName:</td>
				<td><input type="text"  name="username"/></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" name="password"/></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="登陆"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

2.dologin.jsp中设置forward动作以及param指定参数值(增加email参数)

<%@ 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>Insert title here</title>
</head>
<body>
	 <jsp:forward page="user.jsp">
	 	<jsp:param value="Boss" name="username"/>
	 	<jsp:param value="19911007" name="password"/>
	 	<jsp:param value="LoveLay@1007.cn" name="email"/>
	 </jsp:forward>
</body>
</html>

3.user.jsp中输出用户资料

<%@ 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>登陆成功</title>
</head>
<body>
	<h1>用户资料</h1>
    <hr>
    <% 
       request.setCharacterEncoding("utf-8");
       String username = "";
       String password = "";
       String email = "";
       if(request.getParameter("username")!=null)
       {
          username = request.getParameter("username");
       }
       if(request.getParameter("password")!=null)
       {
          password = request.getParameter("password");
       }
       if(request.getParameter("email")!=null)
       {
          email = request.getParameter("email");
       }
       
    %>
        用户名:<%=username %><br>
        密码:<%=password %><br>
        电子邮箱:<%=email %><br>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值