0


超适合练手的一套JavaWeb项目 (超市后台管理系统)

GIF动态图演示

在这里插入图片描述

☁️☁️☁️百度百度网盘提取项目 带数据库![链接]:https://pan.baidu.com/s/13F2rxszZRLGDt9pr6ixYUg提取码:mvc1

一、项目搭建准备工作

  • 1.搭建一个maven web项目
  • 2.配置Tomcat
  • 3.测试项目是否能够跑起来
  • 4.导入项目中遇到的jar包
  • 5.创建项目结构在这里插入图片描述

6.编写实体类

ORM映射:表类映射

7.编写基础公共类

1.数据库配置文件

db.properties文件代码

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username=root
password=111111

2.编写数据库的公共类

java代码

packagecom.syj.dao;importjava.io.IOException;importjava.io.InputStream;importjava.sql.*;importjava.util.Properties;//操作数据库的公共类publicclassBaseDao{privatestaticString driver;privatestaticString url;privatestaticString username;privatestaticString password;//静态代码块,类加载的时候就初始化了static{Properties properties =newProperties();//通过类加载器读取对应的资源InputStream is =BaseDao.class.getClassLoader().getResourceAsStream("db.properties");try{
            properties.load(is);}catch(IOException e){
            e.printStackTrace();}
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");}//获取数据库的链接publicstaticConnectiongetConnection(){Connection connection =null;try{Class.forName(driver);
            connection =DriverManager.getConnection(url, username, password);}catch(Exception e){
            e.printStackTrace();}return  connection;}//编写查询公共类publicstaticResultSetexecute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement )throwsSQLException{
       preparedStatement = connection.prepareStatement(sql);for(int i =0; i < params.length; i++){//setObject,占位符从1开始,但是我们的数组是从0开始!
            preparedStatement.setObject(i+1,params[i]);}

        resultSet = preparedStatement.executeQuery();return resultSet;}//编写增删改查公共方法publicstaticintexecute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement )throwsSQLException{
        preparedStatement = connection.prepareStatement(sql);for(int i =0; i < params.length; i++){//setObject,占位符从1开始,但是我们的数组是从0开始!
            preparedStatement.setObject(i+1,params[i]);}int updateRows = preparedStatement.executeUpdate();return updateRows;}publicstaticbooleancloseResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){boolean flag =true;if(resultSet !=null){try{
                resultSet.close();//GC回收
                resultSet =null;}catch(SQLException e){
                e.printStackTrace();
                flag =false;}}if(preparedStatement !=null){try{
                preparedStatement.close();//GC回收
                preparedStatement =null;}catch(SQLException e){
                e.printStackTrace();
                flag =false;}}if(connection !=null){try{
                connection.close();//GC回收
                connection =null;}catch(SQLException e){
                e.printStackTrace();
                flag =false;}}return flag;}}

3.编写字符编码过滤器

8.导入静态资源

二、登录功能实现

1.编写前端页面

2.设置欢迎界面

xml代码

<!--设置欢迎页面--><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list>

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

java dao层接口代码

packagecom.syj.dao.user;importcom.syj.entity.User;importjava.sql.Connection;importjava.sql.SQLException;publicinterfaceUserDao{//得到登录的用户publicUsergetLoginUser(Connection connection,String userCode)throwsSQLException;}

4.编写dao接口的实现类

java dao接口的实现类代码

packagecom.syj.dao.user;importcom.syj.dao.BaseDao;importcom.syj.entity.User;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassUserDaoImplimplementsUserDao{//得到要登录的用户publicUsergetLoginUser(Connection connection,String userCode)throwsSQLException{PreparedStatement pstm =null;ResultSet rs =null;User user =null;if(connection !=null){String sql ="select * from smbms_user where userCode=?";Object[] params ={userCode};
            rs =BaseDao.execute(connection,pstm,rs,sql,params);if(rs.next()){
                user =newUser();
                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"));}BaseDao.closeResource(null,pstm,rs);}return user;}}

5.业务层接口

java service接口代码

packagecom.syj.service.user;importcom.syj.entity.User;publicinterfaceUserService{//用户登录publicUserlogin(String userCode,String password);}

6.业务层实现类

java serviceImpl实现类代码

packagecom.syj.service.user;importcom.syj.dao.BaseDao;importcom.syj.dao.user.UserDao;importcom.syj.dao.user.UserDaoImpl;importcom.syj.entity.User;importjava.sql.Connection;importjava.sql.SQLException;publicclassUserServiceImplimplementsUserService{privateUserDao userDao;publicUserServiceImpl(){
        userDao =newUserDaoImpl();}publicUserlogin(String userCode,String password){Connection connection =null;User user =null;

        connection =BaseDao.getConnection();try{
            user = userDao.getLoginUser(connection,userCode);}catch(SQLException e){
            e.printStackTrace();}finally{BaseDao.closeResource(connection,null,null);}return user;}}

7.编写Servlet

java 代码

packagecom.syj.servlet;importcom.syj.entity.User;importcom.syj.service.user.UserService;importcom.syj.service.user.UserServiceImpl;importcom.syj.util.Constants;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;publicclassLoginServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{String userCode = req.getParameter("userCode");String userPassword = req.getParameter("userPassword");UserService userService =newUserServiceImpl();User user = userService.login(userCode, userPassword);if(user !=null){
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            resp.sendRedirect("jsp/frame.jsp");}else{
            req.setAttribute("error","用户名或者密码不正确");
            req.getRequestDispatcher("login.jsp").forward(req,resp);}}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doGet(req, resp);}}

8.注册servlet

xml代码

<servlet><servlet-name>LoginServlet</servlet-name><servlet-class>com.syj.servlet.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/login.do</url-pattern></servlet-mapping>

9.测试访问确保以上功能成功

三、登陆功能优化

注销功能:

思路:移除Session,返回登录界面
java servlet代码

packagecom.syj.servlet;importcom.syj.util.Constants;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;publicclassLogoutServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{
      req.getSession().removeAttribute(Constants.USER_SESSION);
      resp.sendRedirect("/login.jsp");}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doGet(req, resp);}}

xml 代码

<!--注销--><servlet><servlet-name>LogoutServlet</servlet-name><servlet-class>com.syj.servlet.LogoutServlet</servlet-class></servlet><servlet-mapping><servlet-name>LogoutServlet</servlet-name><url-pattern>/jsp/logout.do</url-pattern></servlet-mapping>

四、登录拦截优化

编写一个过滤器并注册

java 代码

packagecom.syj.filter;importcom.syj.entity.User;importcom.syj.util.Constants;importjavax.servlet.*;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;publicclassSysFilterimplementsFilter{publicvoidinit(FilterConfig filterConfig)throwsServletException{}publicvoiddoFilter(ServletRequest req,ServletResponse resp,FilterChain chain)throwsIOException,ServletException{HttpServletRequest request =(HttpServletRequest) req;HttpServletResponse response =(HttpServletResponse) resp;User user =(User) request.getSession().getAttribute(Constants.USER_SESSION);if(user==null){
            response.sendRedirect("/smbms/error.jsp");}else{
            chain.doFilter(req,resp);}}publicvoiddestroy(){}}

xml 代码

<!--    用户登录过滤器--><filter><filter-name>SysFilter</filter-name><filter-class>com.syj.filter.SysFilter</filter-class></filter><filter-mapping><filter-name>SysFilter</filter-name><url-pattern>/jsp/*</url-pattern></filter-mapping>

测试,登录,注销,权限,以上功能都要保证执行成功

五、密码修改

1.导入前端素材

pwdmodify.jsp

<li.><a.href=“${pageContext.request.contextPath }/jsp/pwdmodify.jsp”>密码修改</li.>

2.写项目,建议从底层向上写

3.UserDao接口

//修改用户密码publicintupdatePwd(Connection connection,int id,int password)throwsSQLException;

4.UserDao接口实现类

//修改用户密码publicintupdatePwd(Connection connection,int id,int password)throwsSQLException{PreparedStatement pstm =null;int execute =0;if(connection !=null){String sql ="update smbms_user set userPassword = ? where id = ?";Object[] params ={password,id};
            execute =BaseDao.execute(connection,pstm,sql,params);BaseDao.closeResource(null,pstm,null);}return execute;}

5.UserService层

//根据用户Id修改密码publicbooleanupdatePwd(int id,int pwd);

6.UserService实现类

publicbooleanupdatePwd(int id,int pwd){Connection connection =null;boolean flag =false;//修改密码try{
            connection =BaseDao.getConnection();if(userDao.updatePwd(connection,id,pwd)>0){
                flag =true;}}catch(SQLException e){
            e.printStackTrace();}finally{BaseDao.closeResource(connection,null,null);}returnfalse;}

7. servlet控制器层

记得实现复用,需要提取方法

@Overridepackagecom.mario.servlet;importcom.mario.entity.User;importcom.mario.service.user.UserService;importcom.mario.service.user.UserServiceImpl;importcom.mario.util.Constants;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;publicclassUserServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{String method = req.getParameter("method");if(method.equals("savepwd")&& method !=null){this.updatePwd(req,resp);}}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doGet(req, resp);}publicvoidupdatePwd(HttpServletRequest req,HttpServletResponse resp){Object obj = req.getSession().getAttribute(Constants.USER_SESSION);String newpassword = req.getParameter("newpassword");System.out.println(newpassword);boolean flag =false;if(obj !=null&& newpassword !=null){UserService userService =newUserServiceImpl();
            flag = userService.updatePwd(((User) obj).getId(), newpassword);if(flag){
                req.setAttribute("message","修改密码成功");
                req.getSession().removeAttribute(Constants.USER_SESSION);}else{
                req.setAttribute("message","密码修改失败");}}else{
            req.setAttribute("message","新密码有问题");}try{
            req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);}catch(ServletException e){
            e.printStackTrace();}catch(IOException e){
            e.printStackTrace();}}}

8. 测试

优化密码修改使用Ajax

1.阿里巴巴的fastjson

pom.xml

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.61</version></dependency>

2.后台修改的代码

packagecom.mario.servlet;importcom.alibaba.fastjson.JSONArray;importcom.mario.entity.User;importcom.mario.service.user.UserService;importcom.mario.service.user.UserServiceImpl;importcom.mario.util.Constants;importcom.mysql.jdbc.StringUtils;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.Map;publicclassUserServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{String method = req.getParameter("method");if(method.equals("savepwd")&& method !=null){this.updatePwd(req,resp);}elseif(method.equals("pwdmodify")&& method !=null){this.pwdModify(req,resp);}}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doGet(req, resp);}//修改密码publicvoidupdatePwd(HttpServletRequest req,HttpServletResponse resp){Object obj = req.getSession().getAttribute(Constants.USER_SESSION);String newpassword = req.getParameter("newpassword");System.out.println(newpassword);boolean flag =false;if(obj !=null&& newpassword !=null){UserService userService =newUserServiceImpl();
            flag = userService.updatePwd(((User) obj).getId(), newpassword);if(flag){
                req.setAttribute("message","修改密码成功");
                req.getSession().removeAttribute(Constants.USER_SESSION);}else{
                req.setAttribute("message","密码修改失败");}}else{
            req.setAttribute("message","新密码有问题");}try{
            req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);}catch(ServletException e){
            e.printStackTrace();}catch(IOException e){
            e.printStackTrace();}}//验证旧密码,session中有用户的密码publicvoidpwdModify(HttpServletRequest req,HttpServletResponse resp){Object obj = req.getSession().getAttribute(Constants.USER_SESSION);String oldpassword = req.getParameter("oldpassword");Map<String,String> resultMap =newHashMap<String,String>();if(obj==null){
            resultMap.put("result","sessionerror");}elseif(StringUtils.isNullOrEmpty(oldpassword)){
            resultMap.put("result","error");}else{String userPassword =((User) obj).getUserPassword();if(oldpassword.equals(userPassword)){
                resultMap.put("result","true");}else{
                resultMap.put("result","false");}}try{
            resp.setContentType("application/json");PrintWriter writer = resp.getWriter();
            writer.write(JSONArray.toJSONString(resultMap));
            writer.flush();
            writer.close();}catch(IOException e){
            e.printStackTrace();}}}

3.测试

六、用户管理实现

1.获取用户数量

1.UserDao

//查询用户总数publicintgetUserCount(Connection connection,String username,int userRole)throwsSQLException;

2.UserDaoImpl

publicintgetUserCount(Connection connection,String username,int userRole)throwsSQLException{PreparedStatement pstm =null;ResultSet rs =null;int count =0;if(connection !=null){StringBuilder sql =newStringBuilder();
            sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");ArrayList<Object> list =newArrayList<Object>();if(!StringUtils.isNullOrEmpty(username)){
                sql.append(" and u.userName like ? ");
                list.add("%"+username+"%");}if(userRole>0){
                sql.append(" and u.userRole = ? ");
                list.add(userRole);}Object[] params = list.toArray();
            rs =BaseDao.execute(connection,pstm,rs,sql.toString(),params);if(rs.next()){
                count = rs.getInt("count");}BaseDao.closeResource(null,pstm,rs);}return count;}

3.UserService

//查询记录数publicintgetUserCount(String username,int userRole);

4.UserServiceImpl

publicintgetUserCount(String username,int userRole){Connection connection =null;int count =0;try{
            connection =BaseDao.getConnection();
            count = userDao.getUserCount(connection,username,userRole);}catch(SQLException e){
            e.printStackTrace();}finally{BaseDao.closeResource(connection,null,null);}return count;}

2.用户列表页导入

1.UserDao

//通过用户输入的条件查询用户列表publicList<User>getUserList(Connection connection,String userName,int userRole,int currentPageNo,int pageSize)throwsException;

2.UserDaoImpl

//通过用户输入的条件查询用户列表publicList<User>getUserList(Connection connection,String userName,int userRole,int currentPageNo,int pageSize)throwsException{List<User> userList =newArrayList<User>();PreparedStatement pstm=null;ResultSet rs=null;if(connection!=null){StringBuffer sql =newStringBuffer();
            sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");List<Object> list =newArrayList<Object>();if(!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");}if(userRole >0){
                sql.append(" and u.userRole = ?");
                list.add(userRole);}
            sql.append(" order by creationDate DESC limit ?,?");
            currentPageNo =(currentPageNo-1)*pageSize;
            list.add(currentPageNo);
            list.add(pageSize);Object[] params = list.toArray();System.out.println("sql ----> "+ sql.toString());
            rs =BaseDao.execute(connection,pstm,rs,sql.toString(),params);while(rs.next()){User _user =newUser();
                _user.setId(rs.getInt("id"));
                _user.setUserCode(rs.getString("userCode"));
                _user.setUserName(rs.getString("userName"));
                _user.setGender(rs.getInt("gender"));
                _user.setBirthday(rs.getDate("birthday"));
                _user.setPhone(rs.getString("phone"));
                _user.setUserRole(rs.getInt("userRole"));
                _user.setUserRoleName(rs.getString("userRoleName"));
                userList.add(_user);}BaseDao.closeResource(null, pstm, rs);}return userList;}

3.UserService

//根据条件查询用户列表publicList<User>getUserList(String queryUserName,int queryUserRole,int currentPageNo,int pageSize);

4.UserServiceImpl

publicList<User>getUserList(String queryUserName,int queryUserRole,int currentPageNo,int pageSize){Connection connection =null;List<User> userList =null;try{
            connection =BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);}catch(Exception e){
            e.printStackTrace();}finally{BaseDao.closeResource(connection,null,null);}return userList;}

3.获取角色操作

1.RoleDao

//获取角色列表publicList<Role>getRoleList(Connection connection)throwsSQLException;

2.RoleDaoImpl

packagecom.mario.dao.role;importcom.mario.dao.BaseDao;importcom.mario.entity.Role;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;publicclassRoleDaoImplimplementsRoleDao{//获取角色列表publicList<Role>getRoleList(Connection connection)throwsSQLException{PreparedStatement pstm =null;ResultSet rs =null;ArrayList<Role> rolesList =newArrayList<Role>();if(connection !=null){String sql ="select * from smbms_role";Object[] params ={};
            rs =BaseDao.execute(connection,pstm,rs,sql,params);while(rs.next()){Role _role =newRole();
                _role.setId( rs.getInt("id"));
                _role.setRoleCode( rs.getString("roleCode"));
                _role.setRoleName( rs.getString("roleName"));
                rolesList.add(_role);}BaseDao.closeResource(null, pstm, rs);}return rolesList;}}

3.RoleService

//获取角色列表publicList<Role>getRoleList();

4.RoleServiceImpl

packagecom.mario.service.role;importcom.mario.dao.BaseDao;importcom.mario.dao.role.RoleDao;importcom.mario.dao.role.RoleDaoImpl;importcom.mario.entity.Role;importjava.sql.Connection;importjava.util.List;publicclassRoleServiceImplimplementsRoleService{privateRoleDao roleDao;publicRoleServiceImpl(){
        roleDao =newRoleDaoImpl();}//获取角色列表publicList<Role>getRoleList(){Connection connection=null;List<Role> roleList=null;try{
            connection=BaseDao.getConnection();
            roleList = roleDao.getRoleList(connection);}catch(Exception e){
            e.printStackTrace();}finally{BaseDao.closeResource(connection,null,null);}return roleList;}}

4.用户显示的Servlet

  • 获取用户前端的数据(查询)
  • 判断请求是否需要执行,看参数的值判断
  • 为了实现分页,需要计算出当前页面和总页面,页面的大小
  • 用户列表展示
  • 返回前端
packagecom.mario.servlet;importcom.alibaba.fastjson.JSONArray;importcom.mario.entity.Role;importcom.mario.entity.User;importcom.mario.service.role.RoleService;importcom.mario.service.role.RoleServiceImpl;importcom.mario.service.user.UserService;importcom.mario.service.user.UserServiceImpl;importcom.mario.util.Constants;importcom.mario.util.PageSupport;importcom.mysql.jdbc.StringUtils;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.List;importjava.util.Map;publicclassUserServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{String method = req.getParameter("method");if(method.equals("savepwd")&& method !=null){this.updatePwd(req,resp);}elseif(method.equals("pwdmodify")&& method !=null){this.pwdModify(req,resp);}elseif(method.equals("query")&& method !=null){this.query(req,resp);}}@OverrideprotectedvoiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{doGet(req, resp);}publicvoidquery(HttpServletRequest req,HttpServletResponse resp){//接收前端传来的参数String queryUserName = req.getParameter("queryname");String temp = req.getParameter("queryUserRole");//从前端传回来的用户角色码不知是否为空或者是有效角色码,所以暂存起来String pageIndex = req.getParameter("pageIndex");int queryUserRole=0;//通过UserServiceImpl得到用户列表,用户数UserServiceImpl userService =newUserServiceImpl();List<User> userList =null;//用来存储用户列表//设置每页显示的页面容量int pageSize =5;//设置当前的默认页码int currentPageNo=1;//前端传来的参数若不符合查询sql语句,即如果用户不进行设置,值为空会影响sql查询,需要给它们进行一些约束if(queryUserName==null){//这里为空,说明用户没有输入要查询的用户名,则sql语句传值为"",%%,会查询所有记录
            queryUserName="";}if(temp!=null&&!temp.equals("")){//不为空,说明前端有传来的用户所设置的userCode,更新真正的角色码
            queryUserRole=Integer.parseInt(temp);//强制转换,前端传递的参数都是默认字符串,要转成int类型}if(pageIndex!=null){//说明当前用户有进行设置跳转页面
            currentPageNo=Integer.valueOf(pageIndex);}//有了用户名和用户角色后可以开始查询了,所以需要显示当前查询到的总记录条数int totalCount = userService.getUserCount(queryUserName, queryUserRole);//根据总记录条数以及当前每页的页面容量可以算出,一共有几页,以及最后一页的显示条数PageSupport pageSupport =newPageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);//可显示的总页数int totalPageCount=pageSupport.getTotalPageCount();//约束首位页,即防止用户输入的页面索引小于1或者大于总页数if(currentPageNo<1){
            currentPageNo=1;}elseif(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;}//有了,待查询条件,当前页码,以及每页的页面容量后,就可以给出每页的具体显示情况了
         userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
        req.setAttribute("userList",userList);//通过RoleServiceImpl得到角色表RoleService roleService =newRoleServiceImpl();List<Role> roleList = roleService.getRoleList();//用来存储角色表//得到了用户表与角色表以及各种经过处理后的参数,都存进req中
        req.setAttribute("roleList",roleList);
        req.setAttribute("totalCount", totalCount);
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("totalPageCount", totalPageCount);
        req.setAttribute("queryUserName", queryUserName);
        req.setAttribute("queryUserRole", queryUserRole);//将所得到的的所有req参数送回给前端try{
            req.getRequestDispatcher("userlist.jsp").forward(req,resp);}catch(ServletException e){
            e.printStackTrace();}catch(IOException e){
            e.printStackTrace();}}//修改密码publicvoidupdatePwd(HttpServletRequest req,HttpServletResponse resp){Object obj = req.getSession().getAttribute(Constants.USER_SESSION);String newpassword = req.getParameter("newpassword");System.out.println(newpassword);boolean flag =false;if(obj !=null&& newpassword !=null){UserService userService =newUserServiceImpl();
            flag = userService.updatePwd(((User) obj).getId(), newpassword);if(flag){
                req.setAttribute("message","修改密码成功");
                req.getSession().removeAttribute(Constants.USER_SESSION);}else{
                req.setAttribute("message","密码修改失败");}}else{
            req.setAttribute("message","新密码有问题");}try{
            req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);}catch(ServletException e){
            e.printStackTrace();}catch(IOException e){
            e.printStackTrace();}}//验证旧密码,session中有用户的密码publicvoidpwdModify(HttpServletRequest req,HttpServletResponse resp){Object obj = req.getSession().getAttribute(Constants.USER_SESSION);String oldpassword = req.getParameter("oldpassword");Map<String,String> resultMap =newHashMap<String,String>();if(obj==null){
            resultMap.put("result","sessionerror");}elseif(StringUtils.isNullOrEmpty(oldpassword)){
            resultMap.put("result","error");}else{String userPassword =((User) obj).getUserPassword();if(oldpassword.equals(userPassword)){
                resultMap.put("result","true");}else{
                resultMap.put("result","false");}}try{
            resp.setContentType("application/json");PrintWriter writer = resp.getWriter();
            writer.write(JSONArray.toJSONString(resultMap));
            writer.flush();
            writer.close();}catch(IOException e){
            e.printStackTrace();}}}

七、订单管理(增删改查)

八、供应商管理(增删改查)

文章上面的代码只有一部分具体看项目里面的代码…

&

&

&

标签: java tomcat servlet

本文转载自: https://blog.csdn.net/m0_55400356/article/details/125665535
版权归原作者 Mighty CRUD 所有, 如有侵权,请联系我们删除。

“超适合练手的一套JavaWeb项目 (超市后台管理系统)”的评论:

还没有评论