本文简单讲述使用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. Python内置函数(33)——any

    英文文档: any(iterable) Return True if any element of the iterable is true. If the iterable is empty, re ...

  2. Spring知识点回顾(08)spring aware

    Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...

  3. python入门(1)python的前景

    python入门(1)python的前景 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于 ...

  4. C++中const对象和非const对象调用成员函数问题

    一.类MyClass 二.主函数调用 三.结果

  5. SpringMVC(六):@RequestMapping下使用@RequestHeader绑定请求报头的属性值、@CookieValue绑定请求中的Cookie值

    备注:我本地浏览器的报头(Request Header)信息如下: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image ...

  6. Struts(十五):主题

    Theme主题是配置的struts-core.jar下的com.apache.struts2包下的default.properties中(默认配置为:xhtml) ### Standard UI th ...

  7. 闭包(closure)

    大牛的讲解,点击 我们首先需要有作用域的概念,点击 那么什么是闭包? 官方的解释是:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 广义上的 ...

  8. SSM(Spring)中,在工具类中调用服务层的方法

    因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务 ...

  9. Python面向对象——多重继承

    1本文的作用 一个从多个父类继承过来的子类,可以访问所有父类的功能. 2图文介绍 3代码验证 class Contact: all_contacts = [] def __init__(self, n ...

  10. geotrellis使用(三十九)COG 写入更新

    前言 前面介绍过了如何在 ETL 的时候更新 Layer,使得能够在大数据量的时候完成 ETL 操作,同时前两篇文章也介绍了 COG 以及如何在 Geotrellis 中实现 COG 的读取.本文介绍 ...