JSP常用动作(下)

<jsp: setProperty>动作

<jsp: setProperty>动作用来设置、修改已实例化Bean中的属性值。

<jsp:setProperty name="Bean的名称" property="*" |property="属性"[param="属性"|value="值"]/>

1.name属性
name属性是必需的,表示要设置的属性是哪个Bean的,不可接受动态值。
2.property属性
property属性是必需的。表示要设置哪个属性。如果property的值是 *,表示所有名字和Bean属性名字匹配的请求参数都将被传递给相应属性的set方法。
3.param属性
param属性是可选的。指定用哪个请求参数作为Bean属性的值。如果当前请求没有参数,则什么事情也不做,系统不会把null传递给Bean属性的set方法。因此,可以让Bean自己提供默认属性值,只有在请求参数明确指定了新值时才修改默认属性值。

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>动作实例</title>
  </head>
  <body>
  	<p>include动作实例演示!</p>
  	<hr>
  	<jsp:useBean id="time" class="java.util.Date">
  		<jsp:setProperty name="time" property="hours" param="hh"/>
  		<jsp:setProperty name="time" property="minutes" param="mm"/>
  		<jsp:setProperty name="time" property="seconds" param="ss"/>
  	</jsp:useBean>
  	<br>
  	设置属性后的时间:${time }
  	<br>
  	<hr>
  </body>
</html>

在这里插入图片描述
4.value属性
value属性是可选的。该属性用来指定Bean属性的值。
value和param不能同时使用。


<jsp: getProperty>动作

<jsp: getProperty>动作获取指定的Bean属性值并转换成字符串输出。

<jsp:getProperty name="Bean的名称" property="Bean的属性"/>

<jsp: getProperty>元素可以获取Bean的属性值,并可以将其使用或显示在JSP页面中。在使用<jsp: getProperty>之前,必须用<jsp: useBean>创建实例化对象。

1.name属性
name属性指定要获取的Bean名称,不能接受动态值。
2.property属性
property属性指定要获取的Bean名称,不能接受动态值。

用户注册实例:
(假定用户注册信息包含3个参数:用户名、密码、年龄。用户注册页面为register.html,该页面提交后转到数据处理页面register.jsp,并使用JavaBean保存数据。)

注册页面register.html代码:

<html>
  <head>
    <title>用户注册实例</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  
  <body bgcolor="CCEEFF">
  	<div align="center">
  		<form action="register.jsp" method="get">
  			<table border="1">
  				<tr>
  					<th align="center">注册用户信息</th>
  				</tr>
  				<tr>
  					<td>
  						姓名:
  						<input type="text" name="userName" size="16">
  					</td>
  				</tr>
  				<tr>
  					<td>
  						密码:
  						<input type="password" name="password" size="18">
  					</td>
  				</tr>
  				<tr>
  					<td>年龄:<input type="text" name="age" size="16"></td>
  				</tr>
  				<tr>
  					<td><input type=submit value="提交"></td>
  				</tr>
  			</table>
  		</form>
  		<hr>
  	</div>
  </body>
</html>

数据处理页面register.jsp代码:

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>处理用户注册信息页面</title>
  </head>
  
  <body bgcolor="CCEEFF">
  	<jsp:useBean id="user" scope="page" class="test.UserRegisterBean"/>
  	<jsp:setProperty name="user" property="*"/>
  	注册成功:
  	<hr/>
  	使用Bean属性方法:
  	<br/>
  	用户名:<%=user.getUserName()%>
  	<br/>
  	密码:<%=user.getPassword()%>
  	<br/>
  	年龄:<%=user.getAge()%>
  	<hr/>
  	使用getProperty动作:
  	<br/>
  	用户名:<jsp:getProperty name="user" property="userName"/>
  	<br/>
  	密码:<jsp:getProperty name="user" property="password"/>
  	<br/>
  	年龄:<jsp:getProperty name="user" property="age"/>
  	<br/>
  </body>
</html>

UserRegisterBean.java代码:

package test;

public class UserRegisterBean {
		private String userName;
		private String password;
		private int age;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

在这里插入图片描述
在这里插入图片描述


<jsp: forward>动作

<jsp: forward>动作用于转发客户端的请求到另一个页面或者另一个Servlet文件中去。

<jsp:forward page="地址或页面"/>

<jsp: forward>动作可以包含一个或多个<jsp: param>子动作,用于向要引导进入的页面传递参数。当<jsp: forward>动作发生时,如果已经有文本被写入输出流而且页面没有设置缓冲,将抛出异常。

登录实例:
(假定用户名和密码正确,使用<jsp: forward>动作把页面跳转到success.jsp页面,否则返回login.jsp页面)

login.jsp代码:

<%@ page contentType="text/html" pageEncoding="utf-8"%>
<html>
  <head>
    <title>登录页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  
  <body bgcolor="CCEEFF">
  	<div align="center">
  		<form action="logincheck.jsp">
  			<table border=“2">
  				<tr>
  					<td align="center" colspan="2">请登录</td>
  				</tr>
  				<tr>
  					<td>用户名:</td>
  					<td>
  						<input type="text" name="userName" size="16">
  					</td>
  				</tr>
  				<tr>
  					<td>密码:</td>
  					<td>
  						<input type="password" name="password" size="18">
  					</td>
  				</tr>
  				<tr>
  					<td colspan="2"><input type=submit value="登录"></td>
  				</tr>
  			</table>
  		</form>
  	</div>
  </body>
</html>

处理登录页面输入数据的logincheck.jsp页面代码:

<%@ page contentType="text/html" pageEncoding="utf-8"%>
<html>
  <head>
    <title>数据处理页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  
  <body bgcolor="CCEEFF">
  	<%
  		String name=request.getParameter("userName");
  		String password=request.getParameter("password");
  		if(name.equals("aaa")&&password.equals("123456")){
  	%>
  		<jsp:forward page="success.jsp">
  			<jsp:param value="<%=name %>" name="userName"/>
  		</jsp:forward>
  	<%
  		}
  		else{
  	%>
  		<jsp:forward page="login.jsp">
  			<jsp:param name="userName" value="<%=name %>"/>
  		</jsp:forward>
  	<%
  		}
  	 %>
  </body>
</html>

登录成功页面success.jsp代码:

<%@ page contentType="text/html" pageEncoding="utf-8"%>
<html>
  <head>
    <title>登录成功页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  
  <body bgcolor="CCEEFF">
  	登录成功
  	<hr>
  	欢迎您<%=request.getParameter("userName") %>!
  </body>
</html>

在这里插入图片描述当输入正确用户名和密码(aaa和123456),则跳转success界面:
在这里插入图片描述
当输入bbb和123456,则重新跳转登录界面:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值