java/jsp: 登录系统

时间:2024-01-19 12:17:32

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包下的内容:

java/jsp: 登录系统

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");
%>