SMBMS超市訂單管理系統(tǒng)的網(wǎng)站源碼
MVC三層架構(gòu)(代碼整體以此分層編寫)

整體的流程與代碼編寫思路:

建議是從后往前寫,便于調(diào)試與debug,先編寫Dao層,主要負責與數(shù)據(jù)庫交互,編寫sql語句等。然后編寫Servicce層,主要負責調(diào)用Dao層,再編寫Servlet層,其也是主要調(diào)用Service和前端的一些數(shù)據(jù)交互,比如resquet和response等。
基本架構(gòu)

項目搭建準備工作
1- 4

5 創(chuàng)建項目包結(jié)構(gòu)

6-7


8 導(dǎo)致靜態(tài)資源
放在webapp目錄下,因為是網(wǎng)站資源

登錄功能實現(xiàn)

1.編寫前端頁面 login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>系統(tǒng)登錄 - 超市訂單管理系統(tǒng)</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/style.css" rel="external nofollow" />
<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>超市訂單管理系統(tǒng)</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>
2.設(shè)置首頁
<!--設(shè)置歡迎界面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
3.編寫Dao層用戶登錄的接口
Dao層(數(shù)據(jù)持久層)負責操作數(shù)據(jù)庫,業(yè)務(wù)(比如用戶登錄,比對賬號密碼)有業(yè)務(wù)層負責。

public User getLoginUser(Connection connection, String userCode)throws Exception;
4.編寫Dao接口實現(xiàn)類
public User getLoginUser(Connection connection, String userCode) throws Exception {
// TODO Auto-generated method stub
PreparedStatement pstm = null;
ResultSet rs = null;
User user = null;
if(null != connection){
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.setGender(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("createdBy"));
user.setCreationDate(rs.getTimestamp("creationDate"));
user.setModifyBy(rs.getInt("modifyBy"));
user.setModifyDate(rs.getTimestamp("modifyDate"));
}
//connection設(shè)成null不讓其關(guān)閉,是因為后面業(yè)務(wù)層還需要數(shù)據(jù)庫連接。
BaseDao.closeResource(null, pstm, rs);
}
return user;
}
5.業(yè)務(wù)層接口
業(yè)務(wù)層都會調(diào)用Dao層,來獲取業(yè)務(wù)所需的數(shù)據(jù)。

public interface UserService {
//用戶登錄
public User login(String userCode, String userPassword);
}
6.業(yè)務(wù)層實現(xiàn)類
- 因為業(yè)務(wù)層要調(diào)用Dao層(來獲取數(shù)據(jù)庫的數(shù)據(jù)),調(diào)用Dao層,就需要給它傳參數(shù),則connection此時傳給Dao層,所以connection對象在業(yè)務(wù)層創(chuàng)建。
- 業(yè)務(wù)層存在事務(wù),失敗了會回滾。,所以connection對象在業(yè)務(wù)層創(chuàng)建。
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserServiceImpl(){
userDao = new UserDaoImpl();
}
public User login(String userCode, String userPassword) {
// TODO Auto-generated method stub
Connection connection = null;
User user = null;
try {
connection = BaseDao.getConnection();
user = userDao.getLoginUser(connection, userCode);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
BaseDao.closeResource(connection, null, null);
}
//匹配密碼
if(null != user){
if(!user.getUserPassword().equals(userPassword))
user = null;
}
return user;
}
}
7.編寫servlet
- servlet是控制層,用來調(diào)用業(yè)務(wù)層
- 控制層的作用:接受用戶的請求交給業(yè)務(wù)層去做,這里用戶的請求是登錄(輸入用戶名和密碼請求登錄),業(yè)務(wù)層要做的是在數(shù)據(jù)庫中匹配輸入的用戶名和密碼。
public class LoginServlet extends HttpServlet {
//servlet:控制層,接收用戶的請求,調(diào)用業(yè)務(wù)層代碼。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//從前端獲取用戶名和密碼(接收用戶的請求)
String userCode = req.getParameter("userCode");
String userPassword = req.getParameter("userPassword");
//接收請求后需處理業(yè)務(wù),業(yè)務(wù)是:和數(shù)據(jù)庫中的數(shù)據(jù)進行對比,所以需調(diào)用業(yè)務(wù)層
UserServiceImpl userService = new UserServiceImpl();
User user = userService.login(userCode, userPassword);//這里已經(jīng)把登錄的人給查出來了
if(user != null){//查有此人,可以登錄
//將用戶的信息放入Session中
req.getSession().setAttribute(Constant.USER_SESSION , user);
//跳轉(zhuǎn)主頁(跳轉(zhuǎn)到另一個頁面,地址變了,所以用重定向)
resp.sendRedirect("jsp/frame.jsp");
}else{//查無此人,無法登錄
//轉(zhuǎn)發(fā)會登錄頁面,順帶提示它,用戶名或密碼錯誤。((跳轉(zhuǎn)到本頁面,只是在本頁面加了些信息(用戶名或密碼錯誤),地址沒變,所以用請求轉(zhuǎn)發(fā)))
req.setAttribute("error" , "用戶名或密碼錯誤");//請求可以攜帶數(shù)據(jù)
req.getRequestDispatcher("login.jsp").forward(req , resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
8.注冊servlet
<!--servlet-->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.tong.servlet.user.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
整體流程
前端一啟動,執(zhí)行l(wèi)ogin.jsp(設(shè)置成了首頁),直接到web.xml中設(shè)置的login.do(servlet映射),調(diào)用對應(yīng)的控制器servlet(LoginServlet),servlet中會調(diào)用業(yè)務(wù)(UserServiceImpl),然后業(yè)務(wù)會調(diào)用想用的Dao(UserDaoImpl)來獲取數(shù)據(jù)。
登錄功能優(yōu)化
注銷功能:
思路:移除session,返回登錄界面;
LogoutServlet
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().removeAttribute(Constant.USER_SESSION);
resp.sendRedirect(req.getContextPath() + "/login.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注冊
<!--注銷-->
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.tong.servlet.user.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/jsp/logout.do</url-pattern>
</servlet-mapping>
登陸攔截優(yōu)化
為什么要登陸攔截優(yōu)化
- 正常登陸后,將登錄界面的網(wǎng)址復(fù)制下來,在注銷后的登錄界面,黏貼復(fù)制的網(wǎng)址,在沒填用戶名和密碼的前提下,進入了系統(tǒng)。所以需要登錄攔截
攔截判斷的條件是session中有無user這個屬性,因為在用戶注銷,或還沒登錄的情況下,session中沒有user這個屬性,如果沒有,說明不是正常登錄,進行攔截;只有當正常登錄時,會創(chuàng)建session中user這個屬性,此時可以正常登錄。


注銷后,黏貼網(wǎng)址,依然能登錄進來,需進行攔截。

具體步驟
編寫一個過濾器,并注冊
public class SysFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
//過濾器,從session中獲取用戶
User user = (User) request.getSession().getAttribute(Constant.USER_SESSION);
if(user == null){//session已經(jīng)被移除,或者用戶注銷,或還沒登錄
response.sendRedirect("error.jsp");
}else{
chain.doFilter(req , resp);
}
}
public void destroy() {
}
}
<!--用戶登錄過濾器-->
<filter>
<filter-name>SysFilter</filter-name>
<filter-class>com.tong.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SysFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
密碼修改
密碼修改,需要和數(shù)據(jù)庫打交道,所以還需要走dao層、service層、servlet層這一條線。
- Dao層:根據(jù)用戶ID修改用戶密碼(update語句);
- service層:接收傳過來的密碼和調(diào)用Dao獲取后臺的密碼,作對比。
- servlet層:把框里輸入的新舊密碼拿到,交給業(yè)務(wù)層。
1.導(dǎo)入前端素材

2.寫項目,建議從下向上寫

3.編寫Dao層修改當前用戶密碼接口
public interface UserDao里
//修改當前用戶密碼
//增刪改返回的都是int類型,查找返回對應(yīng)的實體類;
public int updatePwd(Connection connection, int id, String pwd)throws Exception;
4.編寫Dao接口實現(xiàn)類
public class UserDaoImpl implements UserDao里
public int updatePwd(Connection connection, int id, String pwd)
throws Exception {
// TODO Auto-generated method stub
int flag = 0;
PreparedStatement pstm = null;
if(connection != null){
String sql = "update smbms_user set userPassword= ? where id = ?";
Object[] params = {pwd,id};
flag = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null, pstm, null);
}
return flag;
}
5.業(yè)務(wù)層接口
public interface UserService里
//根據(jù)userId修改密碼 public boolean updatePwd(int id, String pwd);
6.業(yè)務(wù)層接口實現(xiàn)類
- 因為業(yè)務(wù)層要調(diào)用Dao層(來獲取數(shù)據(jù)庫的數(shù)據(jù)),調(diào)用Dao層,就需要給它傳參數(shù),則connection此時傳給Dao層,所以connection對象在業(yè)務(wù)層創(chuàng)建。
- 業(yè)務(wù)層存在事務(wù),失敗了會回滾。,所以connection對象在業(yè)務(wù)層創(chuàng)建。
public class UserServiceImpl implements UserService里
public boolean updatePwd(int id, String pwd) {
boolean flag = false;//使用標志位,判斷密碼是否修改成功。
Connection connection = null;
try{
connection = BaseDao.getConnection();
if(userDao.updatePwd(connection,id,pwd) > 0)
flag = true;
}catch (Exception e) {
e.printStackTrace();
}finally{
BaseDao.closeResource(connection, null, null);
}
return flag;
}
7.servlet記得實現(xiàn)復(fù)用需要提取出方法
//實現(xiàn)servlet復(fù)用
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if(method.equals("savepwd")){
this.updatePwd(req , resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//從session里拿Uer實體類
Object o = req.getSession().getAttribute(Constant.USER_SESSION);
String newPassword = req.getParameter("newPassword");
boolean flag = false;
if(o != null && newPassword != null && newPassword.length() != 0){//Uer實體類和新密碼newPassword都拿到了,開始調(diào)用業(yè)務(wù)層
UserServiceImpl userService = new UserServiceImpl();
flag = userService.updatePwd(((User) o).getId(), newPassword);
if(flag){
req.setAttribute("message" , "修改密碼成功,請退出,使用新密碼登錄");
//密碼修改成功,當前session。因為密碼變了,session的內(nèi)容自然也變了,所以需要移除,又因為密碼變了需要重新登錄,所以session會重新創(chuàng)建
req.getSession().removeAttribute(Constant.USER_SESSION);
}else {
req.setAttribute("message" , "密碼修改失敗");
}
}else{
req.setAttribute("message" , "新密碼有問題");
}
req.getRequestDispatcher("pwdmodify.jsp").forward(req , resp);
}
}
到此這篇關(guān)于SMBMS超市訂單管理系統(tǒng)的文章就介紹到這了,更多相關(guān)SMBMS超市訂單管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學習使用springboot框架,感興趣的朋友可以了解下2021-04-04
簡單了解Java多態(tài)向上轉(zhuǎn)型相關(guān)原理
這篇文章主要介紹了簡單了解Java多態(tài)向上轉(zhuǎn)型相關(guān)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12
JAVA使用前綴樹(Tire樹)實現(xiàn)敏感詞過濾、詞典搜索
本文主要介紹了JAVA使用前綴樹(Tire樹)實現(xiàn)敏感詞過濾、詞典搜索,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
springboot2.X整合prometheus監(jiān)控的實例講解
這篇文章主要介紹了springboot2.X整合prometheus監(jiān)控的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
深入理解springMVC中的Model和Session屬性
這篇文章主要介紹了深入理解springMVC中的Model和Session屬性,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Springboot如何使用filter對request body參數(shù)進行校驗
這篇文章主要介紹了Springboot如何使用filter對request body參數(shù)進行校驗,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

