Java组件javabean用户登录实例详解
本文简单讲述使用javabean实现用户登录,包括用户登录,注册和退出等。
1.关于javabean
javabean 是一种java语言写成的可重用组件。为写成javabean,类必须是具体的和公共的,并且具有无参数的构造器。javabean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性,set和get方法获取。众所周知,属性名称符合这种模式,其他java 类可以通过自省机制发现和操作这些javabean 的属性。
2.系统架构
2.1登录用例图
2.2页面流程图
2.3系统架构图
2.4数据库设计
本例使用oracle数据库
用户表包括id,用户名,密码,email,共4个字段
-- create table create table p_user ( id varchar2(50) not null, username varchar2(20), password varchar2(20), email varchar2(50) ) tablespace users pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- add comments to the table comment on table p_user is '用户表'; -- add comments to the columns comment on column p_user.id is 'id'; comment on column p_user.username is '用户名'; comment on column p_user.password is '密码'; comment on column p_user.email is 'email';
3.javabean编写
3.1开发数据库底层处理javabean
dbacess.java
package com.baosight.bean; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; /**数据库操作类 * <p>title:dbacess </p> * <p>description:todo </p> * <p>company: </p> * @author yuan * @date 2016-5-22 下午12:40:24*/ public class dbacess { private string driver = "oracle.jdbc.driver.oracledriver"; private string url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:orcl"; private string username = "scott"; private string password = "tiger"; private connection conn; private statement stm; private resultset rs; //创建连接 public boolean createconn() { boolean b = false; try { class.forname(driver);// 加载oracle驱动程序 conn = drivermanager.getconnection(url, username, password); b = true; } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); }// 获取连接 catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return b; } //修改 public boolean update(string sql){ boolean b = false; try { stm = conn.createstatement(); stm.execute(sql); b = true; } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return b; } //查询 public void query(string sql){ try { stm = conn.createstatement(); rs = stm.executequery(sql); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } //判断有无数据 public boolean next(){ boolean b = false; try { if(rs.next()){ b = true; } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return b; } //获取表字段值 public string getvalue(string field) { string value = null; try { if (rs != null) { value = rs.getstring(field); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return value; } //关闭连接 public void closeconn() { try { if (conn != null) { conn.close(); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } //关闭statement public void closestm() { try { if (stm != null) { stm.close(); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } //关闭resultset public void closers() { try { if (rs != null) { rs.close(); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } public string getdriver() { return driver; } public void setdriver(string driver) { this.driver = driver; } public string geturl() { return url; } public void seturl(string url) { this.url = url; } 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 statement getstm() { return stm; } public void setstm(statement stm) { this.stm = stm; } public resultset getrs() { return rs; } public void setrs(resultset rs) { this.rs = rs; } public void setconn(connection conn) { this.conn = conn; } public connection getconn() { return conn; } }
上述数据库操作类使用jdbc连接数据库,并封装了连接数据库、查询、修改、关闭资源等方法。
3.2开发javabean业务逻辑组件
userbean.java
package com.baosight.bean; import com.baosight.util.generateuuid; /** * <p>title:userbean </p> * <p>description:todo </p> * <p>company: </p> * @author yuan * @date 2016-5-22 下午1:05:00*/ public class userbean { //登录验证 public boolean valid(string username,string password){ boolean isvalid = false; dbacess db = new dbacess(); if(db.createconn()){ string sql = "select * from p_user where username='"+username+"' and password='"+password+"'"; db.query(sql); if(db.next()){ isvalid = true; } db.closers(); db.closestm(); db.closeconn(); } return isvalid; } //注册验证 public boolean isexist(string username){ boolean isvalid = false; dbacess db = new dbacess(); if(db.createconn()){ string sql = "select * from p_user where username='"+username+"'"; db.query(sql); if(db.next()){ isvalid = true; } db.closers(); db.closestm(); db.closeconn(); } return isvalid; } //注册用户 public boolean add(string username,string password,string email){ boolean isvalid = false; dbacess db = new dbacess(); if(db.createconn()){ string sql = "insert into p_user(id,username,password,email) values('"+generateuuid.next()+"','"+username+"','"+password+"','"+email+"')"; isvalid = db.update(sql); db.closestm(); db.closeconn(); } return isvalid; } }
上述业务逻辑javabean,定义了登录验证、注册验证和新增用户等方法
3.3关于生成唯一id
上面在新增用户时需要插入id,本例使用uuid来生成唯一id
generateuuid.java
package com.baosight.util; import java.util.date; /** * <p>title:generateuuid </p> * <p>description:todo </p> * <p>company: </p> * @author yuan * @date 2016-5-22 下午1:31:46*/ public class generateuuid { private static date date = new date(); // private static stringbuilder buf = new stringbuilder(); private static int seq = 0; private static final int rotation = 99999; public static synchronized long next(){ if (seq > rotation) seq = 0; // buf.delete(0, buf.length()); date.settime(system.currenttimemillis()); string str = string.format("%1$ty%1$tm%1$td%1$tk%1$tm%1$ts%2$05d", date, seq++); return long.parselong(str); } public static void main(string[] args) { for(int i=0;i<100;i++){ system.out.println(next()); } } }
4.页面编写
4.1登录页面
login.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>登录页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="login_action.jsp" method="post"> <table> <tr> <td colspan="2">登录窗口</td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="username" /> </td> </tr> <tr> <td>密码:</td> <td><input type="text" name="password" /> </td> </tr> <tr> <td colspan="2"><input type="submit" value="登录" /> <a href="register.jsp">注册</a> </td> </tr> </table> </form> </body> </html>
页面效果
4.2登录业务逻辑处理页面
login_action.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ page import="java.sql.*" %> <%@ page import="com.baosight.bean.*" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <% string username = request.getparameter("username"); string password = request.getparameter("password"); if(username==null||"".equals(username.trim())||password==null||"".equals(password.trim())){ //out.write("用户名或密码不能为空!"); system.out.println("用户名或密码不能为空!"); response.sendredirect("login.jsp"); return; //request.getrequestdispatcher("login.jsp").forward(request, response); } userbean userbean = new userbean(); boolean isvalid = userbean.valid(username,password); if(isvalid){ system.out.println("登录成功!"); session.setattribute("username", username); response.sendredirect("welcome.jsp"); return; }else{ system.out.println("用户名或密码错误,登录失败!"); response.sendredirect("login.jsp"); return; } %>
上面的jsp调用了javabean进行业务处理
当用户名或密码为空时返回登录页面login.jsp
当登录成功后将用户信息保存到session,跳转到欢迎页面welcome.jsp
当登录失败时返回登录页面login.jsp
4.3登录成功欢迎页面
welcome.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'welcom.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table> <tr> <td><img src="images/logo4.png" /> </td> <td><img src="images/logo2.png" height="90" /> </td> </tr> <tr> <td colspan="2"><hr /> </td> </tr> <tr> <td> <table> <tr> <td><a>main</a> </td> </tr> <tr> <td><a>menu1</a> </td> </tr> <tr> <td><a>menu2</a> </td> </tr> <tr> <td><a>menu3</a> </td> </tr> <tr> <td><a>menu4</a> </td> </tr> <tr> <td><a>menu5</a> </td> </tr> <tr> <td><a>menu6</a> </td> </tr> <tr> <td><a>menu7</a> </td> </tr> <tr> <td><a>menu8</a> </td> </tr> </table></td> <td> <form action="loginout.jsp" method="post"> <table> <tr> <td colspan="2">登录成功!</td> </tr> <tr> <td>欢迎你,</td> <td>${username }</td> </tr> <tr> <td colspan="2"><input type="submit" value="退出" /></td> </tr> </table> </form></td> </tr> </table> </body> </html>
页面效果
4.4退出登录业务处理页面
loginout.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'loginout.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% session.removeattribute("username"); response.sendredirect("login.jsp"); %> </body> </html>
从session中移除用户信息,跳转到登录页面login.jsp
4.5用户注册页面
register.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>注册页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="register_action.jsp" method="post"> <table> <tr> <td colspan="2">注册窗口</td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>密码:</td> <td><input type="text" name="password1" /></td> </tr> <tr> <td>确认密码:</td> <td><input type="text" name="password2" /></td> </tr> <tr> <td>email:</td> <td><input type="text" name="email" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="注册" /> <a href="login.jsp">返回</a></td> </tr> </table> </form> </body> </html>
运行效果
4.6注册业务处理页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ page import="java.sql.*" %> <%@ page import="com.baosight.bean.*" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <% string username = request.getparameter("username"); string password1 = request.getparameter("password1"); string password2 = request.getparameter("password2"); string email = request.getparameter("email"); if(username==null||"".equals(username.trim())||password1==null||"".equals(password1.trim())||password2==null||"".equals(password2.trim())||!password1.equals(password2)){ //out.write("用户名或密码不能为空!"); system.out.println("用户名或密码不能为空!"); response.sendredirect("register.jsp"); return; //request.getrequestdispatcher("login.jsp").forward(request, response); } userbean userbean = new userbean(); boolean isexit = userbean.isexist(username); if(!isexit){ userbean.add(username, password1, email); system.out.println("注册成功,请登录!"); response.sendredirect("login.jsp"); return; }else{ system.out.println("用户名已存在!"); response.sendredirect("register.jsp"); return; } %>
上面的jsp调用了javabean进行业务处理
当用户名或密码为空时返回注册页面register.jsp
当注册用户名在数据库不存在时,新增用户
新增成功后跳转到登录页面login.jsp
当注册用户名在数据库存在时,返回注册页面register.jsp
5.总结
本例使用javabean对数据库操作和业务逻辑处理进行了封装。
以上即为使用javabean实现用户登录的简单介绍,还需要不断完善,希望大家一起学习进步!