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

  1. 系统结构图

  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

JavaBean实现用户登陆的更多相关文章

  1. 使用jsp+javabean完成用户登陆功能

    User.java package com.po; public class User implements java.io.Serializable { private String usernam ...

  2. 使用Struts框架,实现用户登陆功能

    前言:本篇文章是本人这周学习的一个小结,在自我总结的同时,希望也能够给其他同学带来一点帮助.本文主要知识是参照书本上的知识点以及网上其他博客文章,在上机操练后的所得,具体源码主要来自http://bl ...

  3. 《java入门第一季》模拟用户登陆注册案例集合版

    需求:校验用户名和密码,登陆成功后玩猜数字小游戏. 在这里先写集合版.后面还有IO版.数据库版. 一.猜数字小游戏类: 猜数字小游戏的代码见博客:http://blog.csdn.net/qq_320 ...

  4. 基于用户登陆的struts2中action的分类详解

    在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式. 1.继承 Actio ...

  5. IOS开发之记录用户登陆状态

    上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登 ...

  6. Linux常用命令学习4---(挂载命令mount umount、用户登陆查看和用户交互命令 w who last lastlog)

    紧接着上一篇Linux的命令行的学习:Linux学习3---(文件的压缩和解压缩命令zip unzip tar.关机和重启命令shutdown reboot……) 1.挂载命令     简介      ...

  7. [PHP] - Laravel - 用户登陆中间件

    前言 Laravel 4中,可以使用Route::filter,而在Laravel 5中,没有了filter.php文件,官方建议使用中间件做. 下面是用户登陆的测试例子,涉及到的一些方法和使用,先参 ...

  8. [转]mvc3 使用session来存储类来存储用户登陆信息

    mvc3 使用session来存储类来存储用户登陆信息 2013-08-26 09:48:56|  分类: NET开发 |举报 |字号 订阅   项目之前的登陆机制是这样的:用户登陆后初始化一个类,类 ...

  9. PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: POC http://localhost/p ...

随机推荐

  1. 如何书写高效的css样式

    如何书写高效的css样式? 有以下四个关键要素: 1.高效的css 2.可维护的css 3.组件化的css 4.hack-free  css 书写高效的css: 1.使用外联样式替代行间样式或内嵌样式 ...

  2. java实现红包的分配算法

    个人推测,微信红包在发出的时候已经分配好金额.比如一个10元的红包发给甲乙丙三个人,其实在红包发出去的时候,已经确定了第一个会领取多少,第二个会领取多少金额. 而不是在领取的时候才计算的.下面贴出实现 ...

  3. socket , 套接口还是套接字,傻傻分不清楚

    socket 做网络通信的朋友大都对socket这个词不会感到陌生,但是它的中文翻译是叫套接口还是套接字呢,未必大多数朋友能够分清,今天我们就来聊聊socket的中文名称. socket一词的起源 在 ...

  4. LeetCode & Q121-Best Time to Buy and Sell Stock-Easy

    Array DP Description: Say you have an array for which the ith element is the price of a given stock ...

  5. SQL Server(MySql)中的联合主键(联合索引) 索引分析

    最近有人问到这个问题,之前也一直没有深究联合索引具体使用逻辑,查阅多篇文章,并经过测试,得出一些结论 测试环境:SQL Server 2008 R2 测试结果与MySql联合索引查询机制类似,可以认为 ...

  6. SpringBoot的RestController vs @ResponseBody + @Controller

    @Controller和@RestController的区别?官方文档:@RestController is a stereotype annotation that combines @Respon ...

  7. Linux知识积累(2)dirname的使用方法

    linux中的cd "$(dirname "$0")"/是什么意思呢? 分析如下: 1.$0 表示当前动行的命令名,一般用于shell 脚本中 2.dirnam ...

  8. GIT入门笔记(7)- 修改文件并向版本库提交

    1.修改文件vi readme.txt git status 发现被修改的文件列表git diff readme.txt 2.git add readme.txt git status  --注意gi ...

  9. C#制作ActiveX插件

    首先新建项目--->类库,取名:ActiveXDemo 右键项目属性:应用属性==>程序集信息=>使程序集Com可见, 生成==>输出==>为com互操作注册 新建接口类 ...

  10. python爬虫requests 下载图片

    import requests # 这是一个图片的url url = 'http://yun.itheima.com/Upload/Images/20170614/594106ee6ace5.jpg' ...