SMBMS超市訂單管理系統(tǒng)的網(wǎng)站源碼
MVC三層架構(代碼整體以此分層編寫)
整體的流程與代碼編寫思路:
建議是從后往前
寫,便于調試與debug,先編寫Dao層
,主要負責與數(shù)據(jù)庫交互,編寫sql語句等。然后編寫Servicce層
,主要負責調用Dao層,再編寫Servlet層
,其也是主要調用Service和前端的一些數(shù)據(jù)交互,比如resquet和response等。
基本架構
項目搭建準備工作
1- 4
5 創(chuàng)建項目包結構
6-7
8 導致靜態(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.設置首頁
<!--設置歡迎界面--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>
3.編寫Dao層用戶登錄的接口
Dao層(數(shù)據(jù)持久層)負責操作數(shù)據(jù)庫,業(yè)務(比如用戶登錄,比對賬號密碼)有業(yè)務層負責。
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設成null不讓其關閉,是因為后面業(yè)務層還需要數(shù)據(jù)庫連接。 BaseDao.closeResource(null, pstm, rs); } return user; }
5.業(yè)務層接口
業(yè)務層都會調用Dao層,來獲取業(yè)務所需的數(shù)據(jù)。
public interface UserService { //用戶登錄 public User login(String userCode, String userPassword); }
6.業(yè)務層實現(xiàn)類
- 因為業(yè)務層要調用Dao層(來獲取數(shù)據(jù)庫的數(shù)據(jù)),調用Dao層,就需要給它傳參數(shù),則connection此時傳給Dao層,所以connection對象在業(yè)務層創(chuàng)建。
- 業(yè)務層存在事務,失敗了會回滾。,所以connection對象在業(yè)務層創(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是控制層,用來調用業(yè)務層
- 控制層的作用:接受用戶的請求交給業(yè)務層去做,這里用戶的請求是登錄(輸入用戶名和密碼請求登錄),業(yè)務層要做的是在數(shù)據(jù)庫中匹配輸入的用戶名和密碼。
public class LoginServlet extends HttpServlet { //servlet:控制層,接收用戶的請求,調用業(yè)務層代碼。 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //從前端獲取用戶名和密碼(接收用戶的請求) String userCode = req.getParameter("userCode"); String userPassword = req.getParameter("userPassword"); //接收請求后需處理業(yè)務,業(yè)務是:和數(shù)據(jù)庫中的數(shù)據(jù)進行對比,所以需調用業(yè)務層 UserServiceImpl userService = new UserServiceImpl(); User user = userService.login(userCode, userPassword);//這里已經(jīng)把登錄的人給查出來了 if(user != null){//查有此人,可以登錄 //將用戶的信息放入Session中 req.getSession().setAttribute(Constant.USER_SESSION , user); //跳轉主頁(跳轉到另一個頁面,地址變了,所以用重定向) resp.sendRedirect("jsp/frame.jsp"); }else{//查無此人,無法登錄 //轉發(fā)會登錄頁面,順帶提示它,用戶名或密碼錯誤。((跳轉到本頁面,只是在本頁面加了些信息(用戶名或密碼錯誤),地址沒變,所以用請求轉發(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(設置成了首頁),直接到web.xml中設置的login.do(servlet映射),調用對應的控制器
servlet(LoginServlet),servlet中會調用業(yè)務(UserServiceImpl),然后業(yè)務會調用想用的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)址復制下來,在注銷后的登錄界面,黏貼復制的網(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層:接收傳過來的密碼和調用Dao獲取后臺的密碼,作對比。
- servlet層:把框里輸入的新舊密碼拿到,交給業(yè)務層。
1.導入前端素材
2.寫項目,建議從下向上寫
3.編寫Dao層修改當前用戶密碼接口
public interface UserDao
里
//修改當前用戶密碼 //增刪改返回的都是int類型,查找返回對應的實體類; 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è)務層接口
public interface UserService
里
//根據(jù)userId修改密碼 public boolean updatePwd(int id, String pwd);
6.業(yè)務層接口實現(xiàn)類
- 因為業(yè)務層要調用Dao層(來獲取數(shù)據(jù)庫的數(shù)據(jù)),調用Dao層,就需要給它傳參數(shù),則connection此時傳給Dao層,所以connection對象在業(yè)務層創(chuàng)建。
- 業(yè)務層存在事務,失敗了會回滾。,所以connection對象在業(yè)務層創(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)復用需要提取出方法
//實現(xiàn)servlet復用 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都拿到了,開始調用業(yè)務層 UserServiceImpl userService = new UserServiceImpl(); flag = userService.updatePwd(((User) o).getId(), newPassword); if(flag){ req.setAttribute("message" , "修改密碼成功,請退出,使用新密碼登錄"); //密碼修改成功,當前session。因為密碼變了,session的內容自然也變了,所以需要移除,又因為密碼變了需要重新登錄,所以session會重新創(chuàng)建 req.getSession().removeAttribute(Constant.USER_SESSION); }else { req.setAttribute("message" , "密碼修改失敗"); } }else{ req.setAttribute("message" , "新密碼有問題"); } req.getRequestDispatcher("pwdmodify.jsp").forward(req , resp); } }
到此這篇關于SMBMS超市訂單管理系統(tǒng)的文章就介紹到這了,更多相關SMBMS超市訂單管理系統(tǒng)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot服務設置禁止server.point端口的使用
本文主要介紹了SpringBoot服務設置禁止server.point端口的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-01-01java開發(fā)Dubbo注解Adaptive實現(xiàn)原理
這篇文章主要為大家介紹了java開發(fā)Dubbo注解Adaptive實現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09@Accessors(chain = true)注解報錯的解決方案
這篇文章主要介紹了@Accessors(chain = true)注解報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Struts2實現(xiàn)生成動態(tài)驗證碼并驗證實例代碼
這篇文章主要介紹了Struts2實現(xiàn)生成動態(tài)驗證碼并驗證實例代碼的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06JavaWeb 使用Session實現(xiàn)一次性驗證碼功能
這篇文章主要介紹了JavaWeb 使用Session實現(xiàn)一次性驗證碼功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08