实现原理:
前提:用户表中只有三个字段:ID,用户名,密码;
用户输入用户名,密码和验证码,然后点击登陆,会以“后”方式将该表单提交给后台验证登录的servlet中,servlet的会得到用户输入的用户名,密码(验证码的使用我会在其他博客中给出,今天先不讨论),然后根据用户名到数据库中查询出该用户名下对应的信息(在这里就是密码),如果该用户名不存在则直接提示错误“用户名不存在” ;如果用户名存在,则将用户输入的密码与在数据库中查到的密码进行比较,不同则提示用户“密码错误”,密码相同则再进行一次比较,如果该用户是管理员则跳转到管理员相应页面,如果该用户是普通用户啊则跳转到登录成功页面;
java实现图片验证码链接如下:http :
//blog.csdn.net/lmb55/article/details/46295355
===========================================
具体实现:(基于整体系统的实现)
User.java(一个存放用户信息的javaBean)
UserDao.java(对用户进行操作的接口类,其中定义了一个login(String userName)方法,函数:通过用户名来查询出用户的所有信息,返回一个User用户对象)
UserDaoImpl.java(UserDao接口的实现类)
SystemManager.java(实现系统功能的接口类,在这里是实现了系统的登录功能,其中定义了一个login(String userName,String passWord)方法,功能:调用UserDaoImpl.java中的登录方法通过用户名获得的用户对象,然后与密码进行比较,验证登陆情况)
SystemManagerImpl.java(SystemManager接口的实现类)
LoginServlet.java(验证登录的servlet,功能:获取前台数据,根据用户填写的数据进行相应的跳转)
login.jsp(登陆界面)
DBUtil.java(创建数据库连接的工具类,当需要创建数据库连接时,只需要调用该类的的getConnection()方法即可)
dbconfig.properties(属性文件,数据库连接中需要用到的一些属性常量在这里给出)
这个在过程中很重要的一点一的英文创建³³ 数据库连接,在实现一个系统的过程中,我们要用到很多次数据库连接,每用到一次就创建一次非常麻烦,所以我就把数据库连接单独分离出来,要用到的时候只需要调用相应类的getConnection()方法即可,有关数据库连接的具体实现,我会在另外一篇博客中给出,这里就直接使用了:
链接如下:
http:// blog .csdn.net / lmb55 /条/信息/ 46295325
===========================================
代码如下:
User.java
package com.ymw.domain;
public class User {
private Integer id;
private String name;
private String passWord;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
UserDao.java
package com.ymw.dao;
import com.ymw.domain.User;
public interface UserDao {
public User login(String userName);
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
UserDaoImpl.java
package com.ymw.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.ymw.domain.User;
import com.ymw.exception.DataBaseException;
import com.ymw.util.DBUtil;
public class UserDaoImpl implements UserDao {
@Override
public User login(String userName) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
String sql = "select * from users where name=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, userName);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(userName);
user.setPassword(resultSet.getString("passWord"));
}
} catch (SQLException e) {
e.printStackTrace();
throw new DataBaseException();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return user;
}
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 三十
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
SystemManager.java
package com.ymw.service;
import com.ymw.domain.User;
public interface SystemManager {
public User login(String userName, String passWord);
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
SystemManagerImpl.java
package com.ymw.service;
import com.ymw.dao.UserDao;
import com.ymw.dao.UserDaoImpl;
import com.ymw.domain.User;
import com.ymw.exception.UserNotFoundException;
public class SystemManagerImpl implements SystemManager {
private UserDao userDao=new UserDaoImpl();
@Override
public User login(String userName, String passWord) {
User user = userDao.login(userName);
if(user != null){
if(user.getPassword().equals(passWord)){
// 密码正确
return user;
} else {
// 密码错误
throw new UserNotFoundException("500");
}
} else {
//用户为空
throw new UserNotFoundException("404");
}
}
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 三十
LoginServlet.java
package com.ymw.web.servlet;
import java.io.IOException;
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 com.ymw.domain.User;
import com.ymw.exception.DataBaseException;
import com.ymw.exception.UserNotFoundException;
import com.ymw.service.SystemManager;
import com.ymw.service.SystemManagerImpl;
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 {
request.setCharacterEncoding("utf-8");//解决乱码问题
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
SystemManager systemManager = new SystemManagerImpl();
try {
User user = systemManager.login(userName, passWord);
//把用户的信息放到session中存起来
HttpSession session = request.getSession();
session.setAttribute("user", user);
if(user.getName().equals("admin")){//如果是管理员登陆,则跳到管理员相应界面
response.sendRedirect("admin.jsp");
}else{//如果是普通用户则跳转到登录成功页面
response.sendRedirect("login_success.jsp");
}
} catch (UserNotFoundException e) {
String message="";
if ("500".equals(e.getMessage())) {
message = "密码错误";
} else {
message = "用户不存在";
}
//如果出现错误,则返回到登陆页面,并将用户输入的错误信息带回
request.setAttribute("message", message);
request.setAttribute("userName", userName);
request.setAttribute("passWord", passWord);
request.getRequestDispatcher("login.jsp")
.forward(request, response);
} catch (DataBaseException e) {
e.printStackTrace();
request.getRequestDispatcher("dataBase.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
request.getRequestDispatcher("error.jsp")
.forward(request, response);
}
}
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 三十
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
login.jsp的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录</title>
function checkForm(){
return true;
}
</script>
</head>
<body>
<div>
<div>
<div>
<h1>用户登录</h1>
<form id="loginForm" method="post" action="login.do" onsubmit="return checkForm()">
<table>
${message }
<tr>
<td用户名:</td>
<td><input class="text" type="text" name="userName" value="${userName
}" onfocus="FocusItem(this)" onblur="CheckItem(this);" /><span></span></td>
</tr>
<tr>
<td登录密码:</td>
<td><input class="text" type="password" id="passWord" name="passWord"
value="${passWord }" onfocus="FocusItem(this)" onblur="CheckItem(this);" /><span></span></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="veryCode" onfocus="FocusItem(this)"
onblur="CheckItem(this);" /><img src="" /><span></span></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="立即登录"
/></label></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 三十
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
DBUtil.java
package com.ymw.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
import org.apache.commons.dbcp.BasicDataSource;
public class DBUtil {
public static final String DRIVER_CLASS;
public static final String URL;
public static final String USER_NAME;
public static final String PASSWORD;
private static BasicDataSource bds = new BasicDataSource();
static {
ResourceBundle rb = ResourceBundle
.getBundle("com.ymw.util.dbconfig");
DRIVER_CLASS = rb.getString("DRIVER_CLASS");
URL = rb.getString("URL");
PASSWORD = rb.getString("PASSWORD");
USER_NAME = rb.getString("USER_NAME");
bds.setDriverClassName(DRIVER_CLASS);
bds.setUrl(URL);
bds.setUsername(USER_NAME);
bds.setPassword(PASSWORD);
bds.setInitialSize(100);
bds.setMinIdle(20);
bds.setMaxActive(150);
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = bds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
- 1
- 2
- 3
- 4
- 五
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 三十
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
dbconfig.properties
登陆界面: