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

整體的流程與代碼編寫(xiě)思路:

建議是從后往前寫(xiě),便于調(diào)試與debug,先編寫(xiě)Dao層,主要負(fù)責(zé)與數(shù)據(jù)庫(kù)交互,編寫(xiě)sql語(yǔ)句等。然后編寫(xiě)Servicce層,主要負(fù)責(zé)調(diào)用Dao層,再編寫(xiě)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.編寫(xiě)前端頁(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">用戶(hù)名:</label>
<input type="text" class="input-text" id="userCode" name="userCode" placeholder="請(qǐng)輸入用戶(hù)名" 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.編寫(xiě)Dao層用戶(hù)登錄的接口
Dao層(數(shù)據(jù)持久層)負(fù)責(zé)操作數(shù)據(jù)庫(kù),業(yè)務(wù)(比如用戶(hù)登錄,比對(duì)賬號(hào)密碼)有業(yè)務(wù)層負(fù)責(zé)。

public User getLoginUser(Connection connection, String userCode)throws Exception;
4.編寫(xiě)Dao接口實(shí)現(xiàn)類(lèi)
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 {
//用戶(hù)登錄
public User login(String userCode, String userPassword);
}
6.業(yè)務(wù)層實(shí)現(xiàn)類(lèi)
- 因?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.編寫(xiě)servlet
- servlet是控制層,用來(lái)調(diào)用業(yè)務(wù)層
- 控制層的作用:接受用戶(hù)的請(qǐng)求交給業(yè)務(wù)層去做,這里用戶(hù)的請(qǐng)求是登錄(輸入用戶(hù)名和密碼請(qǐng)求登錄),業(yè)務(wù)層要做的是在數(shù)據(jù)庫(kù)中匹配輸入的用戶(hù)名和密碼。
public class LoginServlet extends HttpServlet {
//servlet:控制層,接收用戶(hù)的請(qǐng)求,調(diào)用業(yè)務(wù)層代碼。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//從前端獲取用戶(hù)名和密碼(接收用戶(hù)的請(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){//查有此人,可以登錄
//將用戶(hù)的信息放入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è)面,順帶提示它,用戶(hù)名或密碼錯(cuò)誤。((跳轉(zhuǎn)到本頁(yè)面,只是在本頁(yè)面加了些信息(用戶(hù)名或密碼錯(cuò)誤),地址沒(méi)變,所以用請(qǐng)求轉(zhuǎn)發(fā)))
req.setAttribute("error" , "用戶(hù)名或密碼錯(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)化
注銷(xiāo)功能:
思路:移除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è)
<!--注銷(xiāo)-->
<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),在注銷(xiāo)后的登錄界面,黏貼復(fù)制的網(wǎng)址,在沒(méi)填用戶(hù)名和密碼的前提下,進(jìn)入了系統(tǒng)。所以需要登錄攔截
攔截判斷的條件是session中有無(wú)user這個(gè)屬性,因?yàn)?code>在用戶(hù)注銷(xiāo),或還沒(méi)登錄的情況下,session中沒(méi)有user這個(gè)屬性,如果沒(méi)有,說(shuō)明不是正常登錄,進(jìn)行攔截;只有當(dāng)正常登錄時(shí),會(huì)創(chuàng)建session中user這個(gè)屬性,此時(shí)可以正常登錄。


注銷(xiāo)后,黏貼網(wǎng)址,依然能登錄進(jìn)來(lái),需進(jìn)行攔截。

具體步驟
編寫(xiě)一個(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中獲取用戶(hù)
User user = (User) request.getSession().getAttribute(Constant.USER_SESSION);
if(user == null){//session已經(jīng)被移除,或者用戶(hù)注銷(xiāo),或還沒(méi)登錄
response.sendRedirect("error.jsp");
}else{
chain.doFilter(req , resp);
}
}
public void destroy() {
}
}
<!--用戶(hù)登錄過(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層這一條線(xiàn)。
- Dao層:根據(jù)用戶(hù)ID修改用戶(hù)密碼(update語(yǔ)句);
- service層:接收傳過(guò)來(lái)的密碼和調(diào)用Dao獲取后臺(tái)的密碼,作對(duì)比。
- servlet層:把框里輸入的新舊密碼拿到,交給業(yè)務(wù)層。
1.導(dǎo)入前端素材

2.寫(xiě)項(xiàng)目,建議從下向上寫(xiě)

3.編寫(xiě)Dao層修改當(dāng)前用戶(hù)密碼接口
public interface UserDao里
//修改當(dāng)前用戶(hù)密碼
//增刪改返回的都是int類(lèi)型,查找返回對(duì)應(yīng)的實(shí)體類(lèi);
public int updatePwd(Connection connection, int id, String pwd)throws Exception;
4.編寫(xiě)Dao接口實(shí)現(xiàn)類(lèi)
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)類(lèi)
- 因?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í)體類(lèi)
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í)體類(lèi)和新密碼newPassword都拿到了,開(kāi)始調(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)文章
springMVC4之強(qiáng)大類(lèi)型轉(zhuǎn)換器實(shí)例解析
本篇文章主要介紹了springMVC4之強(qiáng)大類(lèi)型轉(zhuǎn)換器實(shí)例解析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04
解決idea每次打開(kāi)新的項(xiàng)目都需要重新配置maven問(wèn)題
這篇文章主要介紹了解決idea每次打開(kāi)新的項(xiàng)目都需要重新配置maven問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
簡(jiǎn)單了解Java多態(tài)向上轉(zhuǎn)型相關(guān)原理
這篇文章主要介紹了簡(jiǎn)單了解Java多態(tài)向上轉(zhuǎn)型相關(guān)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java判斷今天,昨天,前天,不能用秒間隔的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java判斷今天,昨天,前天,不能用秒間隔的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
JAVA使用前綴樹(shù)(Tire樹(shù))實(shí)現(xiàn)敏感詞過(guò)濾、詞典搜索
本文主要介紹了JAVA使用前綴樹(shù)(Tire樹(shù))實(shí)現(xiàn)敏感詞過(guò)濾、詞典搜索,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
springboot2.X整合prometheus監(jiān)控的實(shí)例講解
這篇文章主要介紹了springboot2.X整合prometheus監(jiān)控的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
深入理解springMVC中的Model和Session屬性
這篇文章主要介紹了深入理解springMVC中的Model和Session屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲
這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Springboot如何使用filter對(duì)request body參數(shù)進(jìn)行校驗(yàn)
這篇文章主要介紹了Springboot如何使用filter對(duì)request body參數(shù)進(jìn)行校驗(yàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

