注:上课老师教授的关于注册登录功能的实现
下面代码缺少登录,注册,主页面和jdbc.properties,如何与数据库链接还在钻研中
Basedao.java
package dao;
import java.sql.Connection;import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;
public class BaseDao {
// list连接池
static ArrayList<Connection> list = new ArrayList<Connection>();
/**
* 从连接池中获得一个连接
*/
public synchronized static Connection getConnection() throws Exception{
Connection con = null;
// 如果连接池有连接
if (list.size() > 0) {
return list.remove(0);
}
// 连接池中没有连接
else {
Properties p = new Properties();
//加载配置文件
p.load(BaseDao.class.getClassLoader().getResourceAsStream("dao/jdbc.properties"));
String driverClass = p.getProperty("jdbc.driverClass");
String jdbcUrl = p.getProperty("jdbc.jdbcUrl");
String username = p.getProperty("jdbc.username");
String password = p.getProperty("jdbc.password");
//加载驱动
Class.forName(driverClass);
// 和指定的数据库建立连接
for (int i = 0; i < 10; i++) {
con = DriverManager.getConnection(jdbcUrl, username,password);
list.add(con);
}
}
return list.remove(0);
}
/**
* 关闭结果集
* @param rs代表结果集
*/
public static void close(ResultSet rs) throws Exception{
if (rs != null)
rs.close();
}
/**
* 关闭预处理对象
* @param pst代表预处理
*/
public static void close(PreparedStatement pst) throws Exception{
if (pst != null)
pst.close();
}
/**
* 关闭连接对象
* @param con代表连接对象
*/
public synchronized static void close(Connection con) {
if (con != null)
list.add(con);
}
/**
* 关闭结果集,预处理,连接等对象
* @param rs 结果集
* @param ps 预处理
* @param con 连接
*/
public static void close(ResultSet rs, PreparedStatement ps, Connection con) throws Exception{
close(rs);
close(ps);
close(con);
}
}
UserDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDao extends BaseDao{
/**
* 判断email是否可用
*/
public boolean isEmailUse(String email){
boolean b = false;
try {
Connection con = getConnection();//从连接池里获得一个链接
PreparedStatement pst = //(个人理解)创建一个PreparedStatement对象pst,用于存储sql语句,可执行相应方法进行语句修改或者查询
con.prepareStatement("select * from " +
" busertable where bemail = ? ");
pst.setString(1, email);//将?处赋值;
ResultSet rs = pst.executeQuery();//executeQuery方法用于执行查询语句,整句语义为将查询的结果放在结果集对象 rs中;
if(!rs.next()){
b = true;
}
close(rs, pst, con);//实行先开后闭准则;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
/**
* 注册
*/
public boolean register(String email, String pwd){
boolean b = false;
try {
Connection con = getConnection();
PreparedStatement pst =
con.prepareStatement(" insert into " +
" busertable values(?,?,?) ");
pst.setString(1, email);
pst.setString(2, pwd);
pst.setString(3, "0");//0代表未锁定
int n = pst.executeUpdate();
if(n > 0)
b = true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
/**
* 登录
*/
public boolean login(String email, String pwd){
boolean b = false;
try {
Connection con = getConnection();
PreparedStatement pst =
con.prepareStatement("select * from " +
" busertable where bemail = ? and bpwd = ? ");
pst.setString(1, email);
pst.setString(2, pwd);
ResultSet rs = pst.executeQuery();
if(rs.next()){
b = true;
}
close(rs, pst, con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}
LoginServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.UserDao;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String email = request.getParameter("email");
String pwd = request.getParameter("pwd");
UserDao ud = new UserDao();
if(ud.login(email, pwd)){//成功
//session
HttpSession session = request.getSession();
session.setAttribute("useremail", email);
out.print("登录成功!3秒钟跳转到主页面。");
response.setHeader("refresh", "3;url=main.jsp");
}else{//失败
request.setAttribute("errorMessage", "邮箱或密码错误!");
//转发
RequestDispatcher dis =
request.getRequestDispatcher("login.jsp");
dis.forward(request, response);
}
}
RegisterServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
public class RegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
String pwd = request.getParameter("pwd");
String repwd = request.getParameter("repwd");
String errorMessage = null;
//判断密码是否两次输入正确
if(!(pwd==repwd))
{ errorMessage = "密码 确认错误,请重新输入";
//设置状态
request.setAttribute("email", email);
request.setAttribute("errorMessage", errorMessage);
//转发
request.getRequestDispatcher("register.jsp").forward( request, response);
return;
}
//创建一个userdao对象并调用userdao中的isemailuse和regist方法实现注册,用if—else语句实现
UserDao us = new UserDao();
if(!us.isEmailUse(email))
{
errorMessage = "账号已存在,请重新输入";
request.setAttribute("email", email);
request.setAttribute("errorMessage", errorMessage);
//转发
request.getRequestDispatcher("register.jsp").forward( request, response);
return;
}
else
{
us.register(email, repwd);
}
}
}