【数据库创建脚本】
USE weitang;
DROP TABLE IF EXISTS user ;
CREATE TABLE user(
userid VARCHAR(30) PRIMARY KEY ,
name VARCHAR(30) NOT NULL ,
password VARCHAR(32) NOT NULL
) ;
INSERT INTO user (userid,name,password) VALUES ('admin','kyle','12345') ;
【User.java】定义VO类
package im.weitang.vo;
public class User {
private String userid;
private String name;
private String password;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
【DatabaseConnection.java】定义数据库操作类
package im.weitang.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/weitang";
private static final String DBUSER = "root";
private static final String UDPASSWORD = "123456";
private Connection conn = null;
public DatabaseConnection() throws Exception {
try {
Class.forName(DBDRIVER);
this.conn = DriverManager.getConnection(DBURL, DBUSER, UDPASSWORD);
} catch (Exception e) {
throw e;
}
}
public Connection getConnection() {
return this.conn;
}
public void close() throws Exception {
if (this.conn != null) {
try {
this.conn.close();
} catch (Exception e) {
throw e;
}
}
}
}
【IUserDAO.java】定义DAO接口
package im.weitang.dao;
import im.weitang.vo.User;
public interface IUserDAO {
/**
* 用户登录验证
* @param user 传入VO对象
* @return 验证的操作结果
* @throws Exception
*/
public boolean findLogin(User user) throws Exception;
}
【UserDAOImpl.java】定义DAO实现类
package im.weitang.dao.impl;
import im.weitang.dao.IUserDAO;
import im.weitang.vo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDAOImpl implements IUserDAO {
private Connection conn = null;
private PreparedStatement pstmt = null;
public UserDAOImpl(Connection conn) {
this.conn = conn;
}
@Override
public boolean findLogin(User user) throws Exception {
boolean flag = false;
try {
String sql = "SELECT name FROM user WHERE userid=? AND password=?";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1, user.getUserid());
this.pstmt.setString(2, user.getPassword());
ResultSet rs = this.pstmt.executeQuery();
if (rs.next()) {
user.setName(rs.getString(1));
flag = true;
}
} catch (Exception e) {
throw e;
} finally {
if (this.pstmt != null) {
try {
this.pstmt.close();
} catch (Exception e) {
throw e;
}
}
}
return flag;
}
}
【UserDAOProxy.java】定义DAO代理操作类
package im.weitang.dao.proxy;
import im.weitang.dao.IUserDAO;
import im.weitang.dao.impl.UserDAOImpl;
import im.weitang.dbc.DatabaseConnection;
import im.weitang.vo.User;
public class UserDAOProxy implements IUserDAO {
private DatabaseConnection dbc = null;
private IUserDAO dao = null;
public UserDAOProxy() {
try {
this.dbc = new DatabaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
this.dao = new UserDAOImpl(this.dbc.getConnection());
}
@Override
public boolean findLogin(User user) throws Exception {
boolean flag = false;
try {
flag = this.dao.findLogin(user);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return flag;
}
}
【DAOFactory.java】定义工厂类
package im.weitang.factory;
import im.weitang.dao.IUserDAO;
import im.weitang.dao.proxy.UserDAOProxy;
public class DAOFactory {
public static IUserDAO getIUserDAOInstance() {
return new UserDAOProxy();
}
}
【LoginServlet.java】
package im.weitang.servlet;
import im.weitang.factory.DAOFactory;
import im.weitang.vo.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = "login.jsp";
String userid = request.getParameter("userid");
String userpass = request.getParameter("userpass");
List<String> info = new ArrayList<String>();
if (userid == null || "".equals(userid)) {
info.add("用户id不能为空!");
}
if (userpass == null || "".equals(userpass)) {
info.add("密码不能为空");
}
if (info.size() == 0) {
User user = new User();
user.setUserid(userid);
user.setPassword(userpass);
try {
if (DAOFactory.getIUserDAOInstance().findLogin(user)) {
info.add("用户登录成功,欢迎" + user.getName() + "光临!");
} else {
info.add("用户登录失败,用户名或密码错误!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
request.setAttribute("info", info);
request.getRequestDispatcher(path).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
最后修改web.xml,添加如下代码:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>im.weitang.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
【login.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>标题</title>
</head>
<body>
<script>
function validate(f) {
if (!(/^\w{5,15}$/.test(f.userid.value))) {
alert("用户ID必须是5~15位!");
f.userid.focus();
return false;
}
if (!(/^\w{5,15}$/.test(f.userpass.value))) {
alert("密码必须是5~15位!");
f.userpass.focus();
return false;
}
}
</script>
<%
request.setCharacterEncoding("UTF-8");
%>
<%
List<String> info = (List<String>) request.getAttribute("info");
if (info != null) { // 有信息返回
Iterator<String> iter = info.iterator();
while (iter.hasNext()) {
%>
<h4><%=iter.next()%></h4>
<%
}
}
%>
<form action="LoginServlet" method="post"
onSubmit="return validate(this)">
用户ID:<input type="text" name="userid"><br>
密 码:<input type="password" name="userpass"><br>
<input type="submit" value="登陆"> <input type="reset"
value="重置">
</form>
</body>
</html>
网友评论