码迷,mamicode.com
首页 > 其他好文 > 详细

超市订单管理系统,登录功能实现(完整版)

时间:2020-10-13 17:14:50      阅读:20      评论:0      收藏:0      [点我收藏+]

标签:com   首页   resource   required   input   tst   equals   filter   注册   

1.规划登录实现原理流程图

技术图片

2.编写前端页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>系统登录 - 超市订单管理系统</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/style.css" />
    <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>超市订单管理系统</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>
<!--此仅仅是js,想要完整的评论留邮箱-->

3. 设置首页(登录页),将tomcat默认的index改成自己设计的首页

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

 

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

package com.king.dao.user;

import com.king.pojo.User;

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

//面向接口编程,接口是一种约束,要用对应类去实现
public interface UserDao {

    //得到要登陆的用户
    public User getLoginUser(Connection connection,String usercode) throws SQLException;
}

 

5,编写登录接口的实现类

package com.king.dao.user;

import com.king.dao.BaseDao;
import com.king.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.setUserName(rs.getString("userName"));
                    user.setUserPassword(rs.getString("userPassword"));
                    user.setGander(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("createBy"));
                    user.setCreationDate(rs.getTimestamp("creationDate"));
                    user.setModifyBy(rs.getInt("modifyBy"));
                    user.setModifyDate(rs.getTimestamp("modifyDate"));
                }
                BaseDao.closeResource(null,pstm,rs);

        }

        return user;


    }
}

6.业务层接口

package com.king.service.user;

import com.king.pojo.User;

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

 

7业务层接口实现类

package com.king.service.user;

import com.king.dao.BaseDao;
import com.king.dao.user.UserDao;
import com.king.dao.user.UserDaoImpl;
import com.king.pojo.User;

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();
            //通过业务层调用对应的dao层
            user = userDao.getLoginUser(connection, userCode);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }


 }

 

8 编写servlet层,与业务层连接实现数据交互

package com.king.servlet.user;

import com.king.pojo.User;
import com.king.service.user.UserService;
import com.king.service.user.UserServiceImpl;
import com.king.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&&user.getUserPassword().equals((String) userPassword)){//找到数据,可以登录
//将用户的信息放入session中
//为什么放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);
}
}

 

9 注册servlet

<!--Servlet-->
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.king.servlet.user.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login.do</url-pattern>
    </servlet-mapping>
    <!--字符编码过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.king.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

 

 10,注销功能

package com.king.servlet.user;

import com.king.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 LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //移除用户的Constants.USER_SESSION
        req.getSession().removeAttribute(Constants.USER_SESSION);
        resp.sendRedirect("/login.jsp");//返回登陆页面
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

web配置,

<servlet>
        <servlet-name>LogoutServlet</servlet-name>
        <servlet-class>com.king.servlet.user.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogoutServlet</servlet-name>
        <url-pattern>jsp/logout.do</url-pattern>
    </servlet-mapping>

 

超市订单管理系统,登录功能实现(完整版)

标签:com   首页   resource   required   input   tst   equals   filter   注册   

原文地址:https://www.cnblogs.com/CL-King/p/13804820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!