1、理解 JavaBean 的作用;
2、能够定义 JavaBean;
3、能在 JSP 页面中使用 JavaBean。
1、实现功能说明:以用户注册信息页面为例,定义一个用户信息 JavaBean 并在JSP页面中通过jsp中通过直接访问或标签两种形式进行调用。实现下列功能:
(a)创建一个用户注册信息输入页面,用户输入信息包括:姓名、学号、登录密码、性别、年龄、籍贯、选修课程以及个人简介,其中性别为布尔值、选修课程为多选;
(b)定义用户信息JavaBean,其内容与用户注册表单相一致;
(c)创建一个用户信息保持页面,在JSP中使用标签形式,根据前一个页面提交的注册信息构造用户信息JavaBean对象,注意设置其作用范围;
(d)创建一个用户登录以及验证页面,用户通过输入姓名和密码来进行系统登录,当用户名和密码与JavaBean中的信息一致时允许登录,否则输出错误信息或转向错误页面。
2、实验要求:设计代码验证上述方法并调试程序是否正常运行
package com.suke;
public class Student {
private int id;//学号
private String name; //姓名
private String password;//密码
private int age;//年龄
private String gender;//性别
private String address;//籍贯
private String[] course;//选修课程
private String profile;//隔个人简介
public Student() {
}
public Student(int id, String name, String password, int age, String gender, String address, String[] course, String profile) {
this.id = id;
this.name = name;
this.password = password;
this.age = age;
this.gender = gender;
this.address = address;
this.course = course;
this.profile = profile;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String[] getCourse() {
return course;
}
public void setCourse(String[] course) {
this.course = course;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<form action="logon.jsp" method="post">
<input type="submit" value="注册">
</form>
<br>
<form action="login.jsp" method="post">
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<jsp:useBean id="student" class="com.suke.Student" scope="page">
<form action="showInfo.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>学号:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td>籍贯:</td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td>选修课程:</td>
<td><input type="checkbox" name="course" value="Java"/>Java
<input type="checkbox" name="course" value="Python"/>Python
<input type="checkbox" name="course" value="Linux"/>Linux
<input type="checkbox" name="course" value="Vue"/>Vue
</td>
</tr>
<tr>
<td>个人简介:</td>
<td><textarea rows="5" cols="15" name="profile"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="注册"/></td>
</tr>
</table>
</form>
</jsp:useBean>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="student" class="com.suke.Student" scope="page">
<jsp:setProperty name="student" property="*"/>
姓名:
<jsp:getProperty name="student" property="name"/>
<br/>
学号:
<jsp:getProperty name="student" property="id"/>
<br/>
密码:
<jsp:getProperty name="student" property="password"/>
<br/>
性别:
<jsp:getProperty name="student" property="gender"/>
<br/>
年龄:
<jsp:getProperty name="student" property="age"/>
<br/>
籍贯:
<jsp:getProperty name="student" property="address"/>
<br/>
选修课程:
<%
String[] course = student.getCourse();
if (course != null) {
for (int i = 0; i < course.length; i++) {
out.print(course[i] + " ");
}
}
%><br/>
个人简介:
<jsp:getProperty name="student" property="profile"/>
</jsp:useBean>
<br/>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="checkLogin.jsp" method="post">
姓名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>账号面有误,登录失败</h2>
<a href="login.jsp">重新登录</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<%=request.getParameter("name")%>登录成功!
</body>
</html>
**********************
**********************