栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javaweb-31:smbms登录流程实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javaweb-31:smbms登录流程实现

登录功能实现

1.编写前端页面

login.jsp

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


    
        
        系统登录 - 超市订单管理系统
        
        
    
    
        
超市订单管理系统
${error }

别的jsp点击这里下载

2.设置首页,

在web.xml添加如下:


    login.jsp

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

package com.gongyi.dao.user;

import com.gongyi.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

public interface UserDao {
    //得到要登录的用户
    User getLoginUser(Connection connection, String userCode) throws SQLException;
}

4.编写dao层接口的实现类

package com.gongyi.dao.user;

import com.gongyi.dao.baseDao;
import com.gongyi.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDaoImpl implements UserDao {
    //得到要登录的用户
    public User getLoginUser(Connection connection, String userCode) throws SQLException {
        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 = new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                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.setCreateBy(rs.getInt("createBy"));
                user.setCreationDate(rs.getTimestamp("creationDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getTimestamp("modifyDate"));
            }
            baseDao.closeResource(null,pstm,rs);
        }
        return user;
    }
}

5.业务层接口

package com.gongyi.service.user;


import com.gongyi.pojo.User;

public interface UserService {
    //用户登录
    User login(String userCode, String password);
}

6.业务层接口实现类

package com.gongyi.service.user;

import com.gongyi.dao.baseDao;
import com.gongyi.dao.user.UserDao;
import com.gongyi.dao.user.UserDaoImpl;
import com.gongyi.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;


public class UserServiceImpl implements UserService {
    //业务层都会调用Dao层,所以我们要引入dao层
    private UserDao userDao;
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try {
            connection = baseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDao.closeResource(connection,null,null);
        }
        return user;
    }

    @Test
    public void test() {
        UserServiceImpl userService = new UserServiceImpl();
        User admin = userService.login("admin", "123456");
        System.out.println(admin.getUserPassword());
    }
}

1)junit依赖


      junit
      junit
      4.12

2)smbms_user表添加测试数据

 INSERT INTO `smbms`.`smbms_user` (`userCode`, `userName`, `userPassword`, `gender`, `birthday`, `phone`, `address`, `userRole`, `createBy`, `creationDate`, `modifyBy`, `modifyDate`) VALUES ('admin', '系统管理员', '123456', '1', '2021-01-01', '13716267462', '北京', '1', '1', '2021-10-24 18:09:20', '1', '2021-10-24 18:09:25'); 

7.编写servlet

package com.gongyi.servlet.user;


import com.gongyi.pojo.User;
import com.gongyi.service.user.UserService;
import com.gongyi.service.user.UserServiceImpl;
import com.gongyi.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    //Servlet:控制层,调用业务层代码
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet--start...");

        //获取用户名和密码
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        //和数据库中的密码进行对比,调用业务层
        UserService userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);//这里已经把登录的人给查出来了
        if (user != null) {//查有此人,可以登录
            //将用户的信息放到session中
            req.getSession().setAttribute(Constants.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);
    }
}

util/Constants.java

package com.gongyi.util;


public class Constants {
    public final static String USER_SESSION = "userSession";
}

8.注册servlet

在web.xml中添加如下:


    LoginServlet
    com.gongyi.servlet.user.LoginServlet

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

效果图:

1)登录界面

2)登录成功

3)登录失败

彩蛋

1.开发思路

由下到上(dao->service->controller)

2.报错解决

1)Sun Oct 24 18:05:00 CST 2021 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决:

db.properties配置文件中的url添加:useSSL=true&

2)java.sql.SQLException: No database selected

解决:

db.properties配置文件中的url添加:/smbms

3)http://localhost:8080/jsp/frame.jsp 404

去C:UsersAdministrator.IntelliJIdea2019.2systemtomcatUnnamed_smbms_4workCatalinalocalhost目录下无数据

重新rebuild工程

好了

3.web.xml中servlet name报红,打开别的工程web.xml是ok的

在idea关闭这个工程,重启这个工程好了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346212.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号