JavaBean实现用户登陆
本文简单讲述使用javabean实现用户登录,包括用户登录,注册和退出等。
- 系统结构图

2.数据库表
- create table P_USER
- (
- id VARCHAR2(50) not null,
- username VARCHAR2(20),
- password VARCHAR2(20),
- email VARCHAR2(50)
- )
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实现用户登陆的更多相关文章
- 使用jsp+javabean完成用户登陆功能
User.java package com.po; public class User implements java.io.Serializable { private String usernam ...
- 使用Struts框架,实现用户登陆功能
前言:本篇文章是本人这周学习的一个小结,在自我总结的同时,希望也能够给其他同学带来一点帮助.本文主要知识是参照书本上的知识点以及网上其他博客文章,在上机操练后的所得,具体源码主要来自http://bl ...
- 《java入门第一季》模拟用户登陆注册案例集合版
需求:校验用户名和密码,登陆成功后玩猜数字小游戏. 在这里先写集合版.后面还有IO版.数据库版. 一.猜数字小游戏类: 猜数字小游戏的代码见博客:http://blog.csdn.net/qq_320 ...
- 基于用户登陆的struts2中action的分类详解
在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式. 1.继承 Actio ...
- IOS开发之记录用户登陆状态
上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登 ...
- Linux常用命令学习4---(挂载命令mount umount、用户登陆查看和用户交互命令 w who last lastlog)
紧接着上一篇Linux的命令行的学习:Linux学习3---(文件的压缩和解压缩命令zip unzip tar.关机和重启命令shutdown reboot……) 1.挂载命令 简介 ...
- [PHP] - Laravel - 用户登陆中间件
前言 Laravel 4中,可以使用Route::filter,而在Laravel 5中,没有了filter.php文件,官方建议使用中间件做. 下面是用户登陆的测试例子,涉及到的一些方法和使用,先参 ...
- [转]mvc3 使用session来存储类来存储用户登陆信息
mvc3 使用session来存储类来存储用户登陆信息 2013-08-26 09:48:56| 分类: NET开发 |举报 |字号 订阅 项目之前的登陆机制是这样的:用户登陆后初始化一个类,类 ...
- PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: POC http://localhost/p ...
随机推荐
- ASCII排序
ASCII码排序 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入 第一行输 ...
- SpringBoot应用的启动方式
一:IDE 运行Application这个类的main方法 二:在SpringBoot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...
- 大数据学习总结(6)what is our foucus
1.搜索业务 2.画像业务 3.关系图谱 借助es构建搜索.画像和关系图谱
- Extensions in UWP Community Toolkit - Mouse Cursor
概述 UWP Community Toolkit Extensions 中有一个为 Mouse 提供的扩展 - Mouse Cursor Extensions,本篇我们结合代码详细讲解 Mouse C ...
- 一个适用于单页应用,返回原始滚动条位置的demo
如题,最近做一个项目时,由于页面太长,跳转后在返回又回到初始位置,不利于用户体验,需要每次返回到用户离开该页面是的位置.由于是移动端项目,使用了移动端的套ui框架framework7,本身框架的机制是 ...
- 爬虫必备 User-Agent 列表
USER_AGENTS = [ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR ...
- python 类的进阶
类的进阶 一 isinstance(obj,cls)和issubclass(sub,super) class Foo: def __init__(self,name): self.name = nam ...
- Linux:日期用法,及格式定义
在shell脚本中经常会需要获取当前日期的地方,linux的系统时间在shell里是可以直接调用系统变量: 获取今天时期---`date +%Y%m%d` 或 `date +%F` 或 $(date ...
- [转]最常用的15大Eclipse开发快捷键技巧
作者:Java我人生(陈磊兴) 原文出处http://blog.csdn.net/chenleixing/article/details/44600587 做Java开发的,经常会用Eclipse ...
- iOS10 越狱, openSSH
iOS 10 已经可以越狱, 不过比较蛋疼的是非完美越狱,每次重启都要从新越狱. 感兴趣的同学可以尝试一下,本人使用同步推上的教程,亲测可用. 越狱完后想安装OpenSSH, 在Cydia上搜索安装, ...