JSP+JavaBean实现用户注册登录设计,JavaBean主要负责将用户注册信息作为一个整体保存到User类中,User类的各个属性是用户注册页输入的值。
相关知识:
JavaBean编写规范:
JavaBean是公共的类。
构造函数没有输入参数。n
属性必须声明为private,方法必须声明为public。n
用一组set方法设置内部属性。
用一组get方法获取内部属性。
JavaBean是一个没有主方法的类。
JavaBean中可以增加业务方法,来进行数据库的访问操作
一、用户Bean设计
在WEB-INF文件夹下新建classes文件夹,classes下建立myben文件夹
User.java存储于WEB-INF/classes/mybean文件夹
(1)创建User类
package mybean;
public class User
{
private int id;
private String name;
private String password;
private String sex;
private String email;
private String telephone;
private String address;
//添加 爱好属性
private String []aihao;
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 void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
(2)编译类
将cmd.exe复制到classes文件夹下,双击,其默认地址为WEB-INF\classes\
输入 javac mybean\User.java
查看WEB-INF\classes\mybean\下是否产生User.class文件
(3)jsp页面使用Bean
java方式:
<%@ page import=“mybean.User”%>
<% User u=new User();
u.setName("wangxiujuan");
out.println(u.getName());
%>
jsp标签方式:
<%@ page import=“mybean.User”%>
<jsp:useBean id=“u” class=“mybean.User”/>
<jsp:setProperty name=“u” property=”name” value=”wangxiujuan” />
<jsp:getProperty name=“u” property=”name” />
拓展:Myeclipse建立JavaBean
在src文件夹下新建包mybean,右击mybean,新建类class,User
二、实现用户注册功能及应用application保存注册用户
(1)设计注册页面register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<%String msg=(String)request.getAttribute("msg"); %>
<body>
<p>用户注册
<hr/>
<form action="doRegister.jsp" method="post">
<table width="400" border="1">
<tr>
<td>用户名</td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="pwd" /></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="text" name="pwd1" /></td>
</tr>
<tr>
<td>性别</td>
<td><input name="sex" type="radio" value="男" checked="checked" />
男
<input name="sex" type="radio" value="女" />
女</td>
</tr>
<tr>
<td>爱好</td>
<td><input name="aihao" type="checkbox" value="足球" />
足球
<input name="aihao" type="checkbox" value="篮球" />篮球
<input name="aihao" type="checkbox" value="乒乓球" />
乒乓球</td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" /></td>
</tr>
<tr>
<td><%=msg %></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
(2)处理注册信息并显示doRegister.jsp
<%@ page language="java" import="mybean.User" pageEncoding="UTF-8"%>
<%
/*
将上次课获取方式注释掉
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String pwd1 = request.getParameter("pwd1");
String sex=request.getParameter("sex");
String email=request.getParameter("email");
String []aihao=request.getParameterValues("aihao");
*/
// 下边应用JavaBean方式实现获取用户信息,及访问Bean的属性方式实现相关操作
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="u" class="mybean.User"></jsp:useBean>
<jsp:setProperty property="*" name="u"/>
<%
out.print(u.getPwd());
String pwd1 = request.getParameter("pwd1");
String str="";
if(u.getName()==null||u.getPwd()==null)
{
str=str+"用户名或密码不能为空";
}else if(u.getPwd().length()<4||u.getPwd().length()>16){
str=str+"密码长度不在4-16位之间";
}else if(u.getName().length()<4||u.getName().length()>16){
str=str+"用户名 长度不在4-16位之间";
}
if(!u.getPwd().equals(pwd1))
{
str=str+"两次密码不同"+u.getPwd()+":"+pwd1;
}
if(u.getEmail()!=null){
if(u.getEmail().indexOf('@')<0){
str=str+"邮箱不符合要求";
}
}
if(str.length()>1)
{
//实现当输入信息不符合要求时,返回注册页并提示出现问题的信息
request.setAttribute("msg", str);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
%>
<table width="400" border="1">
<tr>
<td>用户名</td>
<td><%=u.getName() %></td>
</tr>
<tr>
<td>密码</td>
<td><%=u.getPwd()%></td>
</tr>
<tr>
<tr>
<td>性别</td>
<td><%=u.getSex()%></td>
</tr>
<tr>
<td>爱好</td>
<td>
<%
if(u.getAihao().length>0)
{
for(String s:u.getAihao()){
out.println(s);
}
}
%>
</td>
</tr>
<tr>
<td>邮箱</td>
<td><%=u.getEmail() %></td>
</tr>
</table>
(3)将用户注册信息Bean保存到application中
在doRegister.jsp 末尾
<%
LinkedList<User> users==(LinkedList<User>)application.getAttribute("users");
if(users==null)
{
users=new LinkedList<User>();
users.add(u);
application.setAttribute("users", users);
}else
{
users.add(u);
}
%>
<%
for(User x:users){
out.println(x.getName());
}
%>
存在问题:
(1)当两次密码不一样是,信息还是存入application对象中
解决:
if(str.length()>1)
{
//实现当输入信息不符合要求时,返回注册页并提示出现问题的信息
request.setAttribute("msg", str);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
之后添加:
else {
将后半部分内容括起来
(2)一个爱好不选择时出现异常
if(u.getAihao()!=null)
三、实现用户登录功能
(1)原有登录界面(无变化)login.jsp
(2)处理登录信息,在application对象中 查找是否有该用户---doLogin.jsp
(原先代码如下)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String name = request.getParameter("username");
String pwd = request.getParameter("password");
out.println(name + ":" + pwd);
//if(name.equals("wangxiujuan"))
if ("wangxiujuan".equals(name)&&"12345".equals(pwd)) {
out.println("用户名:" + name + "密码" + pwd);
out.println("<br/>欢迎登录");
response.setHeader("refresh", "3;url=index.jsp");
session.setAttribute("name", name);
}
else {
out.println("登录失败");
response.setHeader("refresh", "3;url=login.jsp");
}
%>
新代码
<%@ page language="java" import="mybean.User,java.util.*" pageEncoding="UTF-8"%>
<%
String name = request.getParameter("username");
String pwd = request.getParameter("password");
//out.println(name + ":" + pwd);
//if(name.equals("wangxiujuan"))
//判断该用户是否为注册用户
LinkedList<User> users=(LinkedList<User>)application.getAttribute("users");
boolean flag=false;//判断是否查找到该用户
if(users!=null)
{
for(User u:users)
{
//if ("wangxiujuan".equals(name)&&"12345".equals(pwd))
if (u.getName().equals(name)&&u.getPwd().equals(pwd))
{
out.println("用户名:" + name + "密码" + pwd);
out.println("<br/>欢迎登录");
//session.setAttribute("name", name);
session.setAttribute("user", u);
flag=true;
break;
}
}
if(flag)
{
response.setHeader("refresh", "2;url=index.jsp");
}
else {
out.println("登录失败");
response.setHeader("refresh", "2;url=login.jsp");
}
}
%>
跳转到首页:index.jsp
<%@ page language="java" import="java.util.*,mybean.User" pageEncoding="UTF-8"%>
<html>
<body>
<%
//String name=(String)session.getAttribute("name");
User user=(User)session.getAttribute("user");
//if(name==null)
if(user==null)
{
//response.sendRedirect("login.jsp");
out.println("请您先登录");
response.setHeader("refresh", "3;url=login.jsp");
}else{
%>
<p>hello,<%=user.getName() %>
<p><a href="user_find.jsp">查看用户信息</a>
<p align="right"><a href="exit1.jsp">注销</a></p>
<%} %>
</body>
</html>
四、实现登录用户显示个人信息
(1)index.jsp首页添加代码
<p><a href="user_find.jsp">查看用户信息</a>
(2)显示用户信息页user_find.jsp (table部分代码doRegister页相同)
<%
User u=(User)session.getAttribute("user");
if(u!=null)
{
%>
<body>
<table width="400" border="1">
<tr>
<td>用户名</td>
<td><%=u.getName() %></td>
</tr>
<tr>
<td>密码</td>
<td><%=u.getPwd()%></td>
</tr>
<tr>
<tr>
<td>性别</td>
<td><%=u.getSex()%></td>
</tr>
<tr>
<td>爱好</td>
<td>
<%
if(u.getAihao().length>0)
{
for(String s:u.getAihao()){
out.println(s);
}
}
%>
</td>
</tr>
<tr>
<td>邮箱</td>
<td><%=u.getEmail() %></td>
</tr>
</table>
<%} %>
<p><a href="user_update.jsp">更改</a>
</body>
存在问题:当信息中没有邮箱时,输出为NULL
解决:
<tr>
<td>邮箱</td>
<td><%=((u.getEmail()!=null)? u.getEmail():"")%></td>
</tr>
相关知识:条件表达式 int x=10;int y=1; z=(x>y)?x:y;如果x大于y,z的值为x
五、登录用户更改个人信息
(1)user_update.jsp实现更改信息页面,用户名只读,不可更改
<%@ page language="java" import="java.util.*,mybean.User" pageEncoding="UTF-8"%>
<%
User u=(User)session.getAttribute("user");
%>
<form action="user_update1.jsp" method="post">
<table width="400" border="1">
<tr>
<td>用户名</td>
<td><input type="text" name="name" value="<%=u.getName() %>" readonly="true" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="pwd" value="<%=u.getPwd() %>" /></td>
</tr>
<tr>
<td>性别</td>
<td><input name="sex" type="radio" value="男" checked="ok" />
男
<input name="sex" type="radio" value="女" />
女</td>
</tr>
<tr>
<td>爱好</td>
<td><input name="aihao" type="checkbox" value="足球" />
足球
<input name="aihao" type="checkbox" value="篮球" />篮球
<input name="aihao" type="checkbox" value="乒乓球" />
乒乓球</td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email" value="<%=((u.getEmail()!=null)? u.getEmail():"")%>" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="更改" />
</td>
</tr>
</table>
</form>
</body>
</html>
(2)user_update1.jsp实现处理用户更改信息
<%@ page language="java" import="java.util.*,mybean.User" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8") ;
User u=(User)session.getAttribute("user");
String pwd=request.getParameter("pwd");
String sex=request.getParameter("sex");
String []aihao=request.getParameterValues("aihao");
String email=request.getParameter("email");
u.setPwd(pwd);
u.setSex(sex);
u.setAihao(aihao);
u.setEmail(email);
out.println(u.getPwd());
session.setAttribute("user", u);
LinkedList<User> users=(LinkedList<User>)application.getAttribute("users");
for(User u1:users)
{
if(u.getName().equals(u1.getName()))
{
u1=u;
}
}
application.setAttribute("users", users);
response.sendRedirect("index.jsp");
%>
拓展:应用JSP+JavaBean实现留言板开发
左侧为留言界面,上侧为显示留言信息
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.jsp' starting page</title>
</head>
<body>
<form name="form1" method="post" action="doMessage.jsp">
<table>
<tr>
<td> 作者:</td><td><INPUT TYPE="text" NAME="author"></td>
</tr>
<tr>
<td> 留言标题:</td><td><INPUT TYPE="text" NAME="title"></td>
</tr>
<tr>
<td>留言内容:</td><td><TEXTAREA NAME="content" ROWS="4" COLS="20"></TEXTAREA></td>
</tr>
<tr>
<td><INPUT TYPE="submit" value="发表留言"></td>
<td><INPUT TYPE="button" onclick="location.href='showMessage.jsp'" value="查看留言"></td>
</tr>
</table>
</form>
</body>
</html>
(1)用户留言信息是一个整体,一条记录,我们可以应用JavaBean保存
package mybean;
public class Message {
private String author;
private String title;
private String content;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
(2)doMessage.jsp完成处理用户发表留言
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'doMessage.jsp' starting page</title>
</head>
<body>
<%
String author=request.getParameter("author");
String title=request.getParameter("title");
String content=request.getParameter("content");
Message message=new Message();
message.setAuthor(author);
message.setTitle(title);
message.setContent(content);
List<Message> msgList= msgList=(List<Message>)application.getAttribute("msgList");
if(msgList==null)
{
msgList=new LinkedList<Message>();
application.setAttribute("msgList", msgList);
}
msgList.add(message);
response.sendRedirect("message.jsp");
%>
</body>
</html>
此程序需要解决输入中文乱码问题,学生自主完成。
3.showMessage.jsp显示留言信息
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="mybean.*"%>
<html>
<body>
<table border=1>
<tr>
<th width="100">作者</td>
<th width="100">留言标题</td>
<th width="200" >留言内容</td>
</tr>
<%
List<Message> msgList= msgList=(List<Message>)application.getAttribute("msgList");
if( msgList!=null) {
for(Message msg:msgList)
{
%>
<tr>
<td ><%=msg.getAuthor() %></td>
<td ><%=msg.getTitle() %></td>
<td ><%=msg.getContent() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>