1.規划登錄實現原理流程圖

2.編寫前端頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>系統登錄 - 超市訂單管理系統</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/style.css" />
<script type="text/javascript">
/* if(top.location!=self.location){
top.location=self.location;
} */
</script>
</head>
<body class="login_bg">
<section class="loginBox">
<header class="loginHeader">
<h1>超市訂單管理系統</h1>
</header>
<section class="loginCont">
<form class="loginForm" action="${pageContext.request.contextPath }/login.do" name="actionForm" id="actionForm" method="post" >
<div class="info">${error }</div>
<div class="inputbox">
<label for="userCode">用戶名:</label>
<input type="text" class="input-text" id="userCode" name="userCode" placeholder="請輸入用戶名" required/>
</div>
<div class="inputbox">
<label for="userPassword">密碼:</label>
<input type="password" id="userPassword" name="userPassword" placeholder="請輸入密碼" required/>
</div>
<div class="subBtn">
<input type="submit" value="登錄"/>
<input type="reset" value="重置"/>
</div>
</form>
</section>
</section>
</body>
</html>
<!--此僅僅是js,想要完整的評論留郵箱-->
3. 設置首頁(登錄頁),將tomcat默認的index改成自己設計的首頁
<!--設置歡迎頁面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
4.編寫dao層登錄用戶的接口
package com.king.dao.user;
import com.king.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
//面向接口編程,接口是一種約束,要用對應類去實現
public interface UserDao {
//得到要登陸的用戶
public User getLoginUser(Connection connection,String usercode) throws SQLException;
}
5,編寫登錄接口的實現類
package com.king.dao.user;
import com.king.dao.BaseDao;
import com.king.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//實現類,實現接口的方法
public class UserDaoImpl implements UserDao{
public User getLoginUser(Connection connection, String userCode) throws SQLException {
PreparedStatement pstm=null;
ResultSet rs=null;
User user=null;
if (connection!=null){
String sql="select * from smbms_user where userCode=?";
Object[] params={userCode};
rs = BaseDao.execute(connection, pstm, rs, sql, params);
if(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setUserCode(rs.getString("userCode"));
user.setUserName(rs.getString("userName"));
user.setUserPassword(rs.getString("userPassword"));
user.setGander(rs.getInt("gender"));
user.setBirthday(rs.getDate("birthday"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
user.setUserRole(rs.getInt("userRole"));
user.setCreatedBy(rs.getInt("createBy"));
user.setCreationDate(rs.getTimestamp("creationDate"));
user.setModifyBy(rs.getInt("modifyBy"));
user.setModifyDate(rs.getTimestamp("modifyDate"));
}
BaseDao.closeResource(null,pstm,rs);
}
return user;
}
}
6.業務層接口
package com.king.service.user;
import com.king.pojo.User;
public interface UserService {
//用戶登錄
public User login(String userCode,String password);
}
7業務層接口實現類
package com.king.service.user;
import com.king.dao.BaseDao;
import com.king.dao.user.UserDao;
import com.king.dao.user.UserDaoImpl;
import com.king.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
public class UserServiceImpl implements UserService{
//業務層都會調用dao層,所以要引入dao層
private UserDao userDao;
public UserServiceImpl(){
userDao=new UserDaoImpl();
}
public User login(String userCode, String password) {
Connection connection=null;
User user=null;
try {
connection= BaseDao.getConnection();
//通過業務層調用對應的dao層
user = userDao.getLoginUser(connection, userCode);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
}
8 編寫servlet層,與業務層連接實現數據交互
package com.king.servlet.user;
import com.king.pojo.User;
import com.king.service.user.UserService;
import com.king.service.user.UserServiceImpl;
import com.king.util.Constants;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
//Servlet:控制層,調用業務層代碼
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("LoginServlet--start...");
//獲取用戶名和密碼
String userCode= req.getParameter("userCode");
String userPassword= req.getParameter("userPassword");
//和數據庫中的密碼進行對比,調用業務層來實現
UserService userService = new UserServiceImpl();
User user = userService.login(userCode, userPassword);//已經把登錄的人給查出來了
if (user != null&&user.getUserPassword().equals((String) userPassword)){//找到數據,可以登錄
//將用戶的信息放入session中
//為什么放session,區分用戶角色,比如管理員,和普通用戶
req.getSession().setAttribute(Constants.USER_SESSION,user);//鍵值對的形式
//跳轉到主頁
resp.sendRedirect("jsp/frame.jsp");
}else {//查無此人無法登錄
//轉發會登陸頁面,同時提示錯誤
req.setAttribute("error","用戶名或密碼不正確");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
9 注冊servlet
<!--Servlet-->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.king.servlet.user.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<!--字符編碼過濾器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.king.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--設置歡迎頁面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
10,注銷功能
package com.king.servlet.user;
import com.king.util.Constants;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//移除用戶的Constants.USER_SESSION
req.getSession().removeAttribute(Constants.USER_SESSION);
resp.sendRedirect("/login.jsp");//返回登陸頁面
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web配置,
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.king.servlet.user.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>jsp/logout.do</url-pattern>
</servlet-mapping>