SMBMS超市訂單管理系統(tǒng)的網(wǎng)站源碼
MVC三層架構(gòu)(代碼整體以此分層編寫)
整體的流程與代碼編寫思路:
建議是從后往前
寫,便于調(diào)試與debug,先編寫Dao層
,主要負(fù)責(zé)與數(shù)據(jù)庫(kù)交互,編寫sql語(yǔ)句等。然后編寫Servicce層
,主要負(fù)責(zé)調(diào)用Dao層,再編寫Servlet層
,其也是主要調(diào)用Service和前端的一些數(shù)據(jù)交互,比如resquet和response等。
基本架構(gòu)
項(xiàng)目搭建準(zhǔn)備工作
1- 4
5 創(chuàng)建項(xiàng)目包結(jié)構(gòu)
6-7
8 導(dǎo)致靜態(tài)資源
放在webapp目錄下,因?yàn)槭蔷W(wǎng)站資源
登錄功能實(shí)現(xiàn)
1.編寫前端頁(yè)面 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="請(qǐng)輸入用戶名" required/> </div> <div class="inputbox"> <label for="userPassword">密碼:</label> <input type="password" id="userPassword" name="userPassword" placeholder="請(qǐng)輸入密碼" required/> </div> <div class="subBtn"> <input type="submit" value="登錄"/> <input type="reset" value="重置"/> </div> </form> </section> </section> </body> </html>
2.設(shè)置首頁(yè)
<!--設(shè)置歡迎界面--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>
3.編寫Dao層用戶登錄的接口
Dao層(數(shù)據(jù)持久層)負(fù)責(zé)操作數(shù)據(jù)庫(kù),業(yè)務(wù)(比如用戶登錄,比對(duì)賬號(hào)密碼)有業(yè)務(wù)層負(fù)責(zé)。
public User getLoginUser(Connection connection, String userCode)throws Exception;
4.編寫Dao接口實(shí)現(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àn)楹竺鏄I(yè)務(wù)層還需要數(shù)據(jù)庫(kù)連接。 BaseDao.closeResource(null, pstm, rs); } return user; }
5.業(yè)務(wù)層接口
業(yè)務(wù)層都會(huì)調(diào)用Dao層,來(lái)獲取業(yè)務(wù)所需的數(shù)據(jù)。
public interface UserService { //用戶登錄 public User login(String userCode, String userPassword); }
6.業(yè)務(wù)層實(shí)現(xiàn)類
- 因?yàn)闃I(yè)務(wù)層要調(diào)用Dao層(來(lái)獲取數(shù)據(jù)庫(kù)的數(shù)據(jù)),調(diào)用Dao層,就需要給它傳參數(shù),則connection此時(shí)傳給Dao層,所以connection對(duì)象在業(yè)務(wù)層創(chuàng)建。
- 業(yè)務(wù)層存在事務(wù),失敗了會(huì)回滾。,所以connection對(duì)象在業(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是控制層,用來(lái)調(diào)用業(yè)務(wù)層
- 控制層的作用:接受用戶的請(qǐng)求交給業(yè)務(wù)層去做,這里用戶的請(qǐng)求是登錄(輸入用戶名和密碼請(qǐng)求登錄),業(yè)務(wù)層要做的是在數(shù)據(jù)庫(kù)中匹配輸入的用戶名和密碼。
public class LoginServlet extends HttpServlet { //servlet:控制層,接收用戶的請(qǐng)求,調(diào)用業(yè)務(wù)層代碼。 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //從前端獲取用戶名和密碼(接收用戶的請(qǐng)求) String userCode = req.getParameter("userCode"); String userPassword = req.getParameter("userPassword"); //接收請(qǐng)求后需處理業(yè)務(wù),業(yè)務(wù)是:和數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行對(duì)比,所以需調(diào)用業(yè)務(wù)層 UserServiceImpl userService = new UserServiceImpl(); User user = userService.login(userCode, userPassword);//這里已經(jīng)把登錄的人給查出來(lái)了 if(user != null){//查有此人,可以登錄 //將用戶的信息放入Session中 req.getSession().setAttribute(Constant.USER_SESSION , user); //跳轉(zhuǎn)主頁(yè)(跳轉(zhuǎn)到另一個(gè)頁(yè)面,地址變了,所以用重定向) resp.sendRedirect("jsp/frame.jsp"); }else{//查無(wú)此人,無(wú)法登錄 //轉(zhuǎn)發(fā)會(huì)登錄頁(yè)面,順帶提示它,用戶名或密碼錯(cuò)誤。((跳轉(zhuǎn)到本頁(yè)面,只是在本頁(yè)面加了些信息(用戶名或密碼錯(cuò)誤),地址沒(méi)變,所以用請(qǐng)求轉(zhuǎn)發(fā))) req.setAttribute("error" , "用戶名或密碼錯(cuò)誤");//請(qǐng)求可以攜帶數(shù)據(jù) req.getRequestDispatcher("login.jsp").forward(req , resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
8.注冊(cè)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>
整體流程
前端一啟動(dòng),執(zhí)行l(wèi)ogin.jsp(設(shè)置成了首頁(yè)),直接到web.xml中設(shè)置的login.do(servlet映射),調(diào)用對(duì)應(yīng)的控制器
servlet(LoginServlet),servlet中會(huì)調(diào)用業(yè)務(wù)(UserServiceImpl),然后業(yè)務(wù)會(huì)調(diào)用想用的Dao(UserDaoImpl)來(lái)獲取數(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); } }
注冊(cè)
<!--注銷--> <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ù)制下來(lái),在注銷后的登錄界面,黏貼復(fù)制的網(wǎng)址,在沒(méi)填用戶名和密碼的前提下,進(jìn)入了系統(tǒng)。所以需要登錄攔截
攔截判斷的條件是
session中有無(wú)user這個(gè)屬性,因?yàn)?code>在用戶注銷,或還沒(méi)登錄的情況下,session中沒(méi)有user這個(gè)屬性,如果沒(méi)有,說(shuō)明不是正常登錄,進(jìn)行攔截;只有當(dāng)正常登錄時(shí),會(huì)創(chuàng)建session中user這個(gè)屬性,此時(shí)可以正常登錄。
注銷后,黏貼網(wǎng)址,依然能登錄進(jìn)來(lái),需進(jìn)行攔截。
具體步驟
編寫一個(gè)過(guò)濾器,并注冊(cè)
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; //過(guò)濾器,從session中獲取用戶 User user = (User) request.getSession().getAttribute(Constant.USER_SESSION); if(user == null){//session已經(jīng)被移除,或者用戶注銷,或還沒(méi)登錄 response.sendRedirect("error.jsp"); }else{ chain.doFilter(req , resp); } } public void destroy() { } }
<!--用戶登錄過(guò)濾器--> <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ù)庫(kù)打交道,所以還需要走dao層、service層、servlet層這一條線。
- Dao層:根據(jù)用戶ID修改用戶密碼(update語(yǔ)句);
- service層:接收傳過(guò)來(lái)的密碼和調(diào)用Dao獲取后臺(tái)的密碼,作對(duì)比。
- servlet層:把框里輸入的新舊密碼拿到,交給業(yè)務(wù)層。
1.導(dǎo)入前端素材
2.寫項(xiàng)目,建議從下向上寫
3.編寫Dao層修改當(dāng)前用戶密碼接口
public interface UserDao
里
//修改當(dāng)前用戶密碼 //增刪改返回的都是int類型,查找返回對(duì)應(yīng)的實(shí)體類; public int updatePwd(Connection connection, int id, String pwd)throws Exception;
4.編寫Dao接口實(shí)現(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ù)層接口實(shí)現(xiàn)類
- 因?yàn)闃I(yè)務(wù)層要調(diào)用Dao層(來(lái)獲取數(shù)據(jù)庫(kù)的數(shù)據(jù)),調(diào)用Dao層,就需要給它傳參數(shù),則connection此時(shí)傳給Dao層,所以connection對(duì)象在業(yè)務(wù)層創(chuàng)建。
- 業(yè)務(wù)層存在事務(wù),失敗了會(huì)回滾。,所以connection對(duì)象在業(yè)務(wù)層創(chuàng)建。
public class UserServiceImpl implements UserService
里
public boolean updatePwd(int id, String pwd) { boolean flag = false;//使用標(biāo)志位,判斷密碼是否修改成功。 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記得實(shí)現(xiàn)復(fù)用需要提取出方法
//實(shí)現(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實(shí)體類 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實(shí)體類和新密碼newPassword都拿到了,開始調(diào)用業(yè)務(wù)層 UserServiceImpl userService = new UserServiceImpl(); flag = userService.updatePwd(((User) o).getId(), newPassword); if(flag){ req.setAttribute("message" , "修改密碼成功,請(qǐng)退出,使用新密碼登錄"); //密碼修改成功,當(dāng)前session。因?yàn)槊艽a變了,session的內(nèi)容自然也變了,所以需要移除,又因?yàn)槊艽a變了需要重新登錄,所以session會(huì)重新創(chuàng)建 req.getSession().removeAttribute(Constant.USER_SESSION); }else { req.setAttribute("message" , "密碼修改失敗"); } }else{ req.setAttribute("message" , "新密碼有問(wèn)題"); } req.getRequestDispatcher("pwdmodify.jsp").forward(req , resp); } }
到此這篇關(guān)于SMBMS超市訂單管理系統(tǒng)的文章就介紹到這了,更多相關(guān)SMBMS超市訂單管理系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot服務(wù)設(shè)置禁止server.point端口的使用
本文主要介紹了SpringBoot服務(wù)設(shè)置禁止server.point端口的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01java開發(fā)Dubbo注解Adaptive實(shí)現(xiàn)原理
這篇文章主要為大家介紹了java開發(fā)Dubbo注解Adaptive實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09@Accessors(chain = true)注解報(bào)錯(cuò)的解決方案
這篇文章主要介紹了@Accessors(chain = true)注解報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06詳解java倒計(jì)時(shí)三種簡(jiǎn)單實(shí)現(xiàn)方式
這篇文章主要介紹了詳解java倒計(jì)時(shí)三種簡(jiǎn)單實(shí)現(xiàn)方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Struts2實(shí)現(xiàn)生成動(dòng)態(tài)驗(yàn)證碼并驗(yàn)證實(shí)例代碼
這篇文章主要介紹了Struts2實(shí)現(xiàn)生成動(dòng)態(tài)驗(yàn)證碼并驗(yàn)證實(shí)例代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Java并發(fā)編程——volatile關(guān)鍵字
這篇文章主要介紹了Java并發(fā)編程——volatile關(guān)鍵字的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java并發(fā)編程,感興趣的朋友可以了解下2020-10-10JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能
這篇文章主要介紹了JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08