SMBMS超市订单管理系统的网站源码

网友投稿 599 2023-01-22

SMBMS超市订单管理系统的网站源码

SMBMS超市订单管理系统的网站源码

MVC三层架构(代码整体以此分层编写)

整体的流程与代码编写思路:

建议是从后往前写,便于调试与debug,先编写Dao层,主要负责与数据库交互,编写sql语句等。然后编写Servicce层,主要负责调用Dao层,再编写Servlet层,其也是主要调用Service和前端的一些数据交互,比如resquet和response等。

基本架构

项目搭建准备工作

1- 4

DmPKxYfm

5 创建项目包结构

6-7

8 导致静态资源

放在webapp目录下,因为是网站资源

登录功能实现

1.编写前端页面 login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

2.设置首页

login.jsp

3.编写Dao层用户登录的接口

Dao层(数据持久层)负责操作数据库,业务(比如用户登录,比对账号密码)有业务层负责。

public User getLoginUser(Connection connection, String userCode)throws Exception;

4.编写Dao接口实现类

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不让其关闭,是因为后面业务层还需要数据库连接。

BaseDao.closeResource(null, pstm, rs);

}

return user;

}

5.业务层接口

业务层都会调用Dao层,来获取业务所需的数据。

public interface UserService {

//用户登录

public User login(String userCode, String userPassword);

}

6.业务层实现类

因为业务层要调用Dao层(来获取数据库的数据),调用Dao层,就需要给它传参数,则connection此时传给Dao层,所以connection对象在业务层创建。

业务层存在事务,失败了会回滚。,所以connection对象在业务层创建。

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是控制层,用来调用业务层

控制http://层的作用:接受用户的请求交给业务层去做,这里用户的请求是登录(输入用户名和密码请求登录),业务层要做的是在数据库中匹配输入的用户名和密码。

public class LoginServlet extends HttpServlet {

//servlet:控制层,接收用户的请求,调用业务层代码。

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//从前端获取用户名和密码(接收用户的请求)

String userCode = req.getParameter("userCode");

String userPassword = req.getParameter("userPassword");

//接收请求后需处理业务,业务是:和数据库中的数据进行对比,所以需调用业务层

UserServiceImpl userService = new UserServiceImpl();

User user = userService.login(userCode, userPassword);//这里已经把登录的人给查出来了

if(user != null){//查有此人,可以登录

//将用户的信息放入Session中

req.getSession().setAttribute(Constant.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);

}

}

8.注册servlet

LoginServlet

com.tong.servlet.user.LoginServlet

LoginServlet

/login.do

整体流程

前端一启动,执行login.jsp(设置成了首页),直接到web.xml中设置的login.do(servlet映射),调用对应的控制器servlet(LoginServlet),servlet中会调用业务(UserServiceImpl),然后业务会调用想用的Dao(UserDaoImpl)来获取数据。

登录功能优化

注销功能:

思路:移除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);

}

}

注册

LogoutServlet

com.tong.servlet.user.LogoutServlet

LogoutServlet

/jsp/logout.do

登陆拦截优化

为什么要登陆拦截优化

正常登陆后,将登录界面的网址复制下来,在注销后的登录界面,黏贴复制的网址,在没填用户名和密码的前提下,进入了系统。所以需要登录拦截

拦截判断的条件是 session中有无user这个属性,因为在用户注销,或还没登录的情况下,session中没有user这个属性,如果没有,说明不是正常登录,进行拦截;只有当正常登录时,会创建session中user这个属性,此时可以正常登录。

注销后,黏贴网址,依然能登录进来,需进行拦截。

具体步骤

编写一个过滤器,并注册

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已经被移除,或者用户注销,或还没登录

response.sendRedirect("error.jsp");

}else{

chain.doFilter(req , resp);

}

}

public void destroy() {

}

}

SysFilter

com.tong.filter.SysFilter

SysFilter

/jsp/*

密码修改

密码修改,需要和数据库打交道,所以还需要走dao层、service层、servlet层这一条线。

Dao层:根据用户ID修改用户密码(update语句);

service层:接收传过来的密码和调用Dao获取后台的密码,作对比。

servlet层:把框里输入的新旧密码拿到,交给业务层。

1.导入前端素材

2.写项目,建议从下向上写

3.编写Dao层修改当前用户密码接口

public interface UserDao里

//修改当前用户密码

//增删改返回的都是int类型,查找返回对应的实体类;

public int updatePwd(Connection connection, int id, String pwd)throws Exception;

4.编写Dao接口实现类

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.业务层接口

public interface UserService里

//根据userId修改密码

public boolean updatePwd(int id, String pwd);

6.业务层接口实现类

因为业务层要调用Dao层(来获取数据库的数据),调用Dao层,就需要给它传参数,则connection此时传给Dao层,所以connection对象在业务层创建。

业务层存在事务,失败了会回滚。,所以connection对象在业务层创建。

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记得实现复用需要提取出方法

//实现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都拿到了,开始调用业务层

UserServiceImpl userService = new UserServiceImpl();

flag = userService.updatePwd(((User) o).getId(), newPassword);

if(flag){

req.setAttribute("message" , "修改密码成功,请退出,使用新密码登录");

//密码修改成功,当前session。因为密码变了,session的内容自然也变了,所以需要移除,又因为密码变了需要重新登录,所以session会重新创建

req.getSession().removeAttribute(Constant.USER_SESSION);

}else {

req.setAttribute("message" , "密码修改失败");

}

}else{

req.setAttribute("message" , "新密码有问题");

}

req.getRequestDispatcher("pwdmodify.jsp").forward(req , resp);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:什么app运营模式(服务类app的运营模式)
下一篇:MyBatis常用动态sql大总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~