JavaBean实现用户登陆

本文简单讲述使用javabean实现用户登录,包括用户登录,注册和退出等。

  1. 系统结构图

JavaBean实现用户登陆

  2.数据库表

  1. create table P_USER
  2. (
  3. id       VARCHAR2(50) not null,
  4. username VARCHAR2(20),
  5. password VARCHAR2(20),
  6. email    VARCHAR2(50)
  7. )

  3.JavaBean编写

DataAccess.java   数据库操作类使用JDBC连接数据库,并封装了连接数据库、查询、修改、关闭资源等方法

 package data;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class DataAccess {
private String driver="oracle.jdbc.driver.OracleDriver";
private String url="jdbc:oracle:" + "thin:@localhost:1521:orcl";
private String username="C##LYJ";
private String password="lyj123123";
private Connection con;
private Statement stm=null;
private ResultSet rs; 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 steUsername(String username){
this.username=username;
} public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
} public Connection getCon(){
return con;
}
public void steCon(Connection con){
this.con=con;
} 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 boolean creatCon(){
boolean b=false;
try{
Class.forName(driver);//加载oracle驱动程序
con=DriverManager.getConnection(url, username, password);
b=true;
}catch(SQLException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
return b;
}
//修改
public boolean update(String sql){
boolean b=false;
try{
stm=this.con.createStatement();
stm.execute(sql);
b=true;
}catch(SQLException e){
e.printStackTrace();
}
return b;
}
//查询
public void query(String sql){
try{
stm=con.createStatement();
rs=stm.executeQuery(sql);
}
catch(SQLException e){
e.getSQLState();
}
} //判断有无数据
public boolean next(){
boolean b=false;
try{
if(rs.next()){
b=true;
}
}catch(SQLException e){
e.printStackTrace();
}
return b;
}
//获取表字段值
public String getValue(String field){
String value=null;
try{
if(rs!=null){
value=rs.getString(field);
}
}catch(SQLException e){
e.printStackTrace();
}
return value;
} //关闭连接
public void closeCon(){
try{
if(con!=null){
con.close();
}
}catch(SQLException e){
e.printStackTrace();
}
} //关闭Statement
public void closeStm(){
try{
if(stm!=null){
stm.close();
}
}catch(SQLException e){
e.printStackTrace();
}
} //关闭ResultSet
public void closeRs(){
try{
if(rs!=null){
rs.close();
}
}catch(SQLException e){
e.printStackTrace();
}
} }

UserBean.java   对数据库的增加、查询,即定义了登录验证、注册验证和新增用户等方法

 package data;

 public class UserBean {
//登陆验证
public boolean valid(String username,String password){
boolean isValid=false;
DataAccess db=new DataAccess();
if(db.creatCon()){
String sql="select * from p_user where us='"+username+"' and ps='"+password+"'";
db.query(sql);
if(db.next()){
isValid=true;
System.out.print("成功!");
}
db.closeRs();
db.closeStm();
db.closeCon();
}
return isValid;
} //注册验证
public boolean isExist(String username){
boolean isValid=false;
DataAccess db=new DataAccess();
if(db.creatCon()){
String sql="select * from p_user where us='"+username+"'";
db.query(sql);
if(db.next()){
isValid=true;
}
db.closeRs();
db.closeStm();
db.closeCon();
}
return isValid;
}
//注册用户
public boolean add(String username,String password,String email){
boolean isValid=false;
DataAccess db=new DataAccess();
if(db.creatCon()){
String sql= "insert into p_user(id,us,ps,email) values('"+GenerateUnID.next()+"','"+username+"','"+password+"','"+email+"')";
isValid=db.update(sql);
db.closeRs();
db.closeStm();
db.closeCon();
}
return isValid;
}
}

GenerateUnID.java  为每个用户生成唯一ID

 package data;
import java.util.Date; public class GenerateUnID {
private static Date date=new Date();
private static int seq=0;
private static final int ROTATION=99999;
public static synchronized long next(){
if(seq>ROTATION) seq=0;
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.JSP页面编写

    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>My JSP 'index.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>
<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="password" 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"%>
<%
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 'login_action.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>
<%@ page import="data.*" %>
<%@ page import="java.sql.*" %> <%
String us=request.getParameter("username");
String ps=request.getParameter("password");
if(us==null||"".equals(us.trim())||ps==null||"".equals(ps.trim())){
System.out.print("用户名或者密码不能为空!");
response.sendRedirect("index.jsp"); } UserBean userBean=new UserBean();
boolean isValid=userBean.valid(us, ps);
System.out.print(isValid);
if(isValid){
System.out.println("登陆成功!");
session.setAttribute("username", us);
response.sendRedirect("welcom.jsp");
}
else{
System.out.print("用户名或者密码错误!");
response.sendRedirect("login.jsp");
} %>
</body>
</html>

    4.3 登陆欢迎界面 welcom.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>
<form action="loginout.jsp" method="post">
<table>
<tr>
<td colspan="2">登陆成功</td>
</tr>
<tr>
<td>欢迎你:</td>
<td><%=session.getAttribute("username")%></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="退出"/></td>
</tr>
</table>
</form>
</body>
</html>

    4.4  用户注册页面 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>My JSP 'register.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>
<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="password" name="password1"></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" 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.4 注册页面逻辑处理 register_action.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="data.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'register_action.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> <%
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)||password2==null||"".equals(password2)||!password1.equals(password2)){
System.out.print("用户名或密码不能为空!");
response.sendRedirect("register.jsp");
return;
}
UserBean userbean=new UserBean();
boolean isExit=userbean.isExist(username);
if(!isExit){
userbean.add(username, password1, email);
System.out.print("注册成功,请登陆!");
response.sendRedirect("login.jsp"); }
else{
System.out.print("用户名已存在!");
response.sendRedirect("register.jsp"); }
%>
</body>
</html>

    4.5  退出页面

 <%@ 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>

    5. 总结

      主要是为了使用javabean对数据库操作和业务逻辑处理进行了封装,例子很简单,由小及大,掌握思想即可。

源码:https://github.com/lyj8330328/JavaBean

上一篇:基于JQuery EasyUI的WebMVC控件封装(含源码)


下一篇:[python]数据整理,将取得的众多的沪深龙虎榜数据整一整