db类

 
 package db;
import java.sql.*; import javax.naming.InitialContext;
import javax.sql.DataSource; public class DBConnect { private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rst;
private String str1; private void init()
{
try {
InitialContext ctx = new InitialContext();
DataSource dsSource = (DataSource) ctx.lookup("java:comp/env/jdbc/test");
conn = dsSource.getConnection(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public DBConnect()
{
//构造函数
try {
//获得一个数据库连接 init(); stmt = conn.createStatement(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //执行数据库查询语句,string s为sql语句
public void excuteQuery(String s)
{ try {
if(stmt != null)
{
rst = stmt.executeQuery(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } //对数据库执行update操作
public int excuteUpdate(String s)
{
int status = ;
try {
if(stmt != null)
status = stmt.executeUpdate(s);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return status;
} //以下为赋值方法
//字符串赋值
public void setString(int i, String s)
{
try {
pstmt.setString(i, s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //boolean赋值
public void setBoolean(int i, Boolean flag)
{
try {
pstmt.setBoolean(i, flag);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //date日期型赋值
public void setDate(int i, Date date) {
try {
pstmt.setDate(i, date);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //时间类型赋值
public void setTime(int i, Time time) {
try {
pstmt.setTime(i, time);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //short类型赋值
public void setShort(int i, Short short1)
{
try {
pstmt.setShort(i, short1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //整形赋值
public void setInt(int i, int int1)
{
try {
pstmt.setInt(i, int1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //长整型赋值
public void setLong(int i, long l)
{
try {
pstmt.setLong(i, l);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //浮点型赋值
public void setFloat(int i, float f)
{
try {
pstmt.setFloat(i, f);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //double类型赋值
public void setDouble(int i, double d)
{
try {
pstmt.setDouble(i, d);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //以下为获取的方法
//获取字符串
public String getString(int i) throws SQLException
{ return rst.getString(i);
}
public String getString(String str1) throws SQLException
{
return rst.getString(str1);
} //取得boolean类型
public boolean getBoolean(int i) throws SQLException
{
return rst.getBoolean(i);
}
public boolean getBoolean(String str1) throws SQLException
{
return rst.getBoolean(str1);
} //取得date日期型
public Date getDate(int i) throws SQLException
{
return rst.getDate(i);
} //取得日期字符型
public Date getDate(String str1) throws SQLException{
return rst.getDate(str1);
} //获取time时间
public Time getTime(int i) throws SQLException{
return rst.getTime(i);
} //获取time字符时间
public Time geTime(String str1) throws SQLException
{
return rst.getTime(str1);
} //获取双精度型
public double getDouble(int i) throws SQLException
{
return rst.getDouble(i);
}
public double getDouble(String str1) throws SQLException
{
return rst.getDouble(str1);
} //获取浮点型
public float getFloat(int i) throws SQLException{
return rst.getFloat(i);
}
public float getFloat(String str1) throws SQLException
{
return rst.getFloat(str1);
} //获取int型
public int getInt(int i) throws SQLException
{
return rst.getInt(i);
}
public int getInt(String str1) throws SQLException
{
return rst.getInt(str1); } //获取长整型
public long getLong(int i) throws SQLException
{
return rst.getLong(i);
}
public long getLong(String str1) throws SQLException
{
return rst.getLong(str1);
} //获取short短整型
public short getShort(int i) throws SQLException
{
return rst.getShort(i);
}
public short getShort(String str1) throws SQLException
{
return rst.getShort(str1);
} //指针下移一位
public boolean next()
{
try {
return rst.next();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
} //释放内存
public void close()
{ try {
if(conn != null)
conn.close();
if(stmt != null)
stmt.close();
if(rst!=null)
rst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}

register包下的内容:

package register;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateFormat { public static long getDate(String s)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition parsePosition = new ParsePosition(0);
Date date = simpleDateFormat.parse(s, parsePosition);
return date.getTime(); } }

  

 package register;
import register.product.Product;
import register.product.SqlProduct;
import register.user.SqlUser;
import register.user.User; public abstract class Factory { private static Factory factory = null;
public Factory(){}
public static Factory getInstance()
{
if(factory == null)
{
try {
Class factoryClass = Class.forName("register.SqlFactory");
factory = (Factory) factoryClass.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } return factory;
} public abstract User initUser();
public abstract SqlUser initSqlUser();
public abstract Product initProduct();
public abstract SqlProduct initSqlProduct(); }
 package register;
import register.product.Product;
import register.product.SqlProduct;
import register.user.SqlUser;
import register.user.User;; public class SqlFactory extends Factory{ public User initUser(){
return new SqlUser();
} public Product initProduct()
{
return new SqlProduct();
} public SqlUser initSqlUser()
{
return new SqlUser();
} public SqlProduct initSqlProduct()
{
return new SqlProduct();
}
}

register.user包
user.java

 package register.user;

 public interface User {
public abstract void setUser_id(String user_id);
public abstract String getUser_id();
public abstract void setPassword(String password);
public abstract String getPassword();
public abstract void setName(String name);
public abstract String getName();
public abstract void setSex(String sex);
public abstract String getSex();
public abstract void setBirth(long birth);
public abstract long getBirth();
public abstract void setDescription(String deString);
public abstract String getDescription(); }

abstractUser.java

 package register.user;

 public abstract class AbstractUser implements User {

     private String user_id;
private String password;
private String name;
private String sex;
private long birth;
private String description; public void setUser_id(String user_id)
{
this.user_id = user_id;
} public String getUser_id()
{
return this.user_id;
} public void setPassword(String password)
{
this.password = password;
} public String getPassword()
{
return this.password;
} public void setName(String name)
{
this.name = name;
} public String getName()
{
return this.name;
} public void setSex(String sex)
{
this.sex = sex;
} public String getSex()
{
return this.sex;
} public void setBirth(long birth)
{
this.birth = birth;
} public long getBirth()
{
return this.birth;
} public void setDescription(String description)
{
this.description = description;
} public String getDescription()
{
return this.description;
} }

sqlUser.java

 package register.user;
import register.Factory;
import db.DBConnect; public class SqlUser extends AbstractUser
{ public boolean checkRPwd(String password, String rpassword)
{
if( password != null && rpassword != null )
{
if( password.equals(rpassword) )
{
return true;
}else{
return false;
}
}else{
return false;
}
} public boolean saveUser(User user)
{
String User_id = user.getUser_id();
String Password = user.getPassword();
String Name = user.getName();
String Sex = user.getSex();
long Birth = user.getBirth();
String Description = user.getDescription();
String str = "insert into users values('" + User_id +"', '"+ Password +"', '"+ Name +"', '"+ Sex +"', " +
"'"+ Birth +"', '"+ Description +"')"; try{
DBConnect dbcon = new DBConnect();
dbcon.excuteUpdate(str);
return true;
}catch(Exception e)
{
e.printStackTrace();
return false;
}
} public User getUser(String ID)
{
User user = Factory.getInstance().initUser();
String str = "select * from users where USER_ID = '"+ID+"'"; try{
DBConnect dbConnect = new DBConnect();
dbConnect.excuteQuery(str);
if(dbConnect.next())
{ user.setUser_id(dbConnect.getString());
user.setPassword(dbConnect.getString());
user.setName(dbConnect.getString());
user.setSex(dbConnect.getString());
user.setBirth(dbConnect.getLong());
user.setDescription(dbConnect.getString()); }
}catch(Exception e)
{
e.printStackTrace();
} return user;
} public int checkUser(String ID, String password)
{
int index = -;
User user = getUser(ID);
if(user != null)
{
if( user.getPassword().equals(password) )
{
index = ;
}else{
index = ;
}
}else{
index = ;
}
return index;
} }

jsp文件

index

 <%@ page contentType="text/html; charset=utf-8" %>
<%@page import="java.util.Iterator"%>
<%@ page import="register.product.SqlProduct" %>
<%@ page import="register.product.Product" %>
<%@ page import="register.Factory" %> <html>
<title>登录系统</title>
<body>
<h2>Hello World!</h2> <%
String user_id = (String)session.getAttribute("user_id");
if(user_id == "" || user_id == null)
{
response.sendRedirect("login.jsp");
} Product product = null;
SqlProduct sqlProduct = Factory.getInstance().initSqlProduct();
Iterator iterator = sqlProduct.getProduct(); String product_name = "";
float price;
String description = "";
%>
<table>
<tr>
<td>用户登录系统</td>
<td>
<%
if("".equals(user_id))
{
%>
<a href="login.jsp">登录</a>,
<a href="register.jsp">注销</a>
<%
}else{ %>
<%=user_id %>你好,<a href="logout.jsp">退出</a>
<%
}
%> </td>
</tr>
<%
while( iterator.hasNext() )
{
product = (Product)iterator.next();
product_name = product.getProduct_name();
price = product.getPrice();
description = product.getDescription();
%>
<tr>
<td>产品名称:<%=product_name %></td>
<td>价格:<%=price %></td>
</tr>
<tr>
<td cospan="">说明:<%=description %></td>
</tr>
<% }
%> </table>
</body>
</html>

login.jsp

 <%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>login</title>
</head>
<body> <form action="do_login.jsp" method="post">
<table>
<tr>
<td colspan="" align="center">登录</td>
</tr>
<%
String info = request.getParameter("info");
String errMsg = "";
if("".equals(info))
{
errMsg = "用户名错误";
}else if("".equals(info))
{
errMsg = "密码错误";
}
if( !"".equals(errMsg))
{
%>
<tr>
<td colspan="" align="center" color="red"><%=errMsg %></td>
</tr>
<%
}
%>
<tr>
<td>用户名</td>
<td><input type="text" name="user_id"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="passwd"></td>
</tr>
<tr>
<td colspan="" align="center"><input type="submit" value="登录"></td>
</tr> <tr>
<td colspan="" align="right">
<a href="register.jsp">注册</a>
</td>
</tr>
</table>
</form> </body>
</html>

do_login.jsp

 <%@ page import="register.user.User"%>
<%@ page import="register.user.SqlUser"%>
<%@ page import="register.Factory"%>
<%
String user_id = request.getParameter("user_id");
String passwd = request.getParameter("passwd"); SqlUser sqlUser = Factory.getInstance().initSqlUser();
int index = sqlUser.checkUser(user_id, passwd);
if( index != - )
{
if(index == )
{
response.sendRedirect("login.jsp?info=1");
}else if(index == )
{
response.sendRedirect("login.jsp?info=2");
}else{
session.setAttribute("user_id", user_id);
response.sendRedirect("index.jsp");
}
}else{
response.sendRedirect("login.jsp");
}
%>

register.jsp

 <%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>register</title>
</head>
<body> <form action="do_register.jsp" method="post">
<table> <tr>
<td colspan="" align="center">注册</td>
</tr>
<tr>
<td>用户名</td>
<td><input type="text" name="user_id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="passwd"></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="password" name="rpasswd"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" value="" name="sex">男
<input type="radio" value="" name="sex">女
</td>
</tr>
<tr>
<td>生日</td>
<td>

<select name="year">
<%
int i=;
for(i=; i < ; i++)
{
%>
<option value="<%=i%>"><%=i %></option>
<%
}
%>
</select> 月
<select name="month">
<%
for(i=; i < ; i++)
{
%>
<option value="<%=i %>"><%=i %></option>
<%
}
%>
</select>

<select name="day">
<%
for(i=; i < ; i++)
{
%>
<option value="<%=i%>"><%=i%></option>
<%
}
%>
</select>
</td>
</tr>
<tr>
<td>自我介绍</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td colspan=""><input type="submit" value="注册"></td>
</tr>
<tr>
<td colspan="" align="right"><a href="login.jsp">登录</a></td>
</tr> </table>
</form> </body>
</html>

do_register.jsp

 <%@ page import="register.DateFormat"%>
<%@ page import="register.user.User"%>
<%@ page import="register.user.SqlUser"%>
<%@ page import="register.Factory"%>
<%
//获取传递过来的参数
String user_id = request.getParameter("user_id");
String passwd = request.getParameter("passwd");
String rpasswd = request.getParameter("rpasswd");
String sex = request.getParameter("sex");
String name = request.getParameter("name"); String year = request.getParameter("year");
String month = request.getParameter("month");
if(month.length() ==)month = ""+month;
String day = request.getParameter("day");
if(day.length() ==)day = ""+day;
String date = year+"-"+month+"-"+day; String description = request.getParameter("description"); //初始化user实例
User user = Factory.getInstance().initUser();
//初始化sqlUser实例
SqlUser sqlUser = Factory.getInstance().initSqlUser(); //将日期转为Long型
long birth = DateFormat.getDate(date);
if( sqlUser.checkRPwd(passwd, rpasswd) )
{ user.setUser_id(user_id);
user.setPassword(passwd);
user.setName(name);
user.setSex(sex);
user.setBirth(birth);
user.setDescription(description); if(sqlUser.saveUser(user))
{
session.setAttribute("user_id", user_id);
response.sendRedirect("index.jsp"); }else{
response.sendRedirect("register.jsp");
} }else{
response.sendRedirect("register.jsp");
} %>

logout.jsp

<%
session.removeAttribute("user_id");
response.sendRedirect("index.jsp");
%>

  

java/jsp: 登录系统的更多相关文章

  1. Java模拟登录系统抓取内容【转载】

    没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下....     为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以 ...

  2. java单点登录系统CAS的简单使用

    转:http://blog.csdn.net/yunye114105/article/details/7997041 背景 有几个相对独立的java的web应用系统, 各自有自己的登陆验证功能,用户在 ...

  3. 纯jsp用户登录系统

    用纯jsp技术实现用户登录系统,需要用到三个.jsp文件.在文本目录下新建三个.jsp文件,分别命名为login.jsp,logincl.jsp和wel.jsp. 1.login.jsp文件用来放界面 ...

  4. java jsp实现网络考试系统(mysql)

    java网络考试系统 功能:可进行学生.管理员登录,学生考试.管理员出卷.列表分页 @ 目录 java网络考试系统 实现效果 主要代码实现 写在最后 实现效果 主要代码实现 package cn.it ...

  5. Java实现:服务端登录系统并跳转到系统内的指定页面(不调用浏览器)

    Java实现:服务端登录系统并跳转到系统内的指定页面(不调用浏览器) 1,思路:根据爬虫思想: 2,代码: /** * ClassName:AuthFr * Function: TODO * Reas ...

  6. java 不同意同一账户不同IP 同一时候登录系统解决的方法 兼容IE Firefox

    需求就是 不同意同一个账户同一时间登录系统.仅仅要有一个账户在线其它人就是不能用这个账户. 功能非常easy,过程非常纠结 . 这篇文章攻克了兼容IE.Firefox 浏览器下,不同IP 地址 同一用 ...

  7. Java 简单登录MVC

    构建一个简单的基于MVC模式的JavaWeb 零晨三点半了,刚刚几个兄弟一起出去吼歌,才回来,这应该是我大学第二次去K歌,第一次是大一吧,之后每次兄弟喊我,我都不想去,因为我还是很害怕去KTV,或许是 ...

  8. 单点登录系统CAS筹建及取得更多用户信息的实现

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  9. CAS实现的单点登录系统

    单点登录(single sign on ),简称SSO. 纯属学习用,对来自网络的部分如果侵害了您的权力,请联系我.QQ:262800095 SSO的定义是在多个应用系统中,用户只需要登录一次就可以访 ...

随机推荐

  1. 学习即语言的学习 “表示” “presentation”

    w 龚升

  2. 网络安装CentOS6.4

    第一步:所需工具安装包下载地址: http://115.com/file/antbtamu#网络安装CentOS.rar(或者下载NetbootM.exe和hfs.exe) 第二步:将CentOS6. ...

  3. 【转】虚拟 IO 服务器(VIOS)和 IBM i

    Power 主机上的虚拟化应用,简单阐述虚拟 IO 服务器的功能,用途,优点,以及虚拟 IO 服务器在高级虚拟化技术的作用.举例说明虚拟 IO 服务器与 IBM i 分区直接互联特性. 引言 随着信息 ...

  4. 将电脑中编写的app网页放到手机上访问

    http://jingyan.baidu.com/article/3065b3b6e5becdbecff8a4d5.html 1.在控制面板-管理工具找不到IIS,则先在程序-打开或关闭window功 ...

  5. window 注册表上下文菜单如何配置?

    注册表结构? Keys Abbreviation Description 描述 HKEY_CLASSES_ROOT HKCR Stores file association and COM objec ...

  6. velocity 的 escape实现

    EscapeHtmlReference的escape方法调用以下方法实现: StringEscapeUtils.escapeHtml(param); 再调用 org.apache.commons.la ...

  7. JavaScript中的Date,RegExp,Function对象

    Date对象 创建Date对象 //方法1:不指定参数var nowd1=new Date();alert(nowd1.toLocaleString( ));//方法2:参数为日期字符串var now ...

  8. mssql 中文乱码 字库集 问题解决方法

    The database could not be exclusively locked to perform the operation(SQL Server 5030错误解决办法)   SQL S ...

  9. 每天一个linux命令(6/18):lsof命令

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以,lsof的功 ...

  10. 活用:after 让图片垂直居中

    现在莫名虽然更喜欢 background 但大多时候还是选择用 img,这其中的利弊争议不在本文中赘述. 那么在布局中常会遇到定高容器中图片居中的需求,这时就有很多方法了呀: line-height ...