ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

登录功能实现

2022-06-01 19:01:05  阅读:116  来源: 互联网

标签:功能 userCode 登录 rs 实现 user import null User


登录功能实现

img

编写前端登录页面

1.编写页面代码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>系统登录 - 超市订单管理系统</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>

2.把这个登录页面设置为首页

如果不设置的话,默认首页为index.jsp

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

Dao 层

1.编写Dao层得到用户登录的接口

import com.xy.pojo.User;
import java.sql.Connection;

public interface UserDao {
    //从数据库查询指定的用户,这里不需要获取连接数据库对象,交给业务层去做
    public User getLoginUser(Connection connection, String userCode);
}

2.编写Dao层的实现类

public class UserDaoImpl implements UserDao {
     public User getLoginUser(Connection connection, String userCode) throws SQLException {
         PreparedStatement statement = null;
         ResultSet rs = null;
         User user = null;
         if (connection != null){
             String sql = "select * from `smbms_user` where userCode=?";
             Object[] param = {userCode};
             rs = BaseDao.execute(connection, sql, param, statement, rs);
             while (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.setModifyBy(rs.getInt("modifyBy"));
                 user.setModifyDate(rs.getDate("modifyDate"));
             }
             BaseDao.close(null,statement,rs);
         }
         return user;
     }
}

业务层

1.编写业务层接口

import com.xy.pojo.User;

import java.sql.SQLException;

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

2.编写业务层实现类

public class UserServiceImpl implements UserService{

    //业务层都会调用Dao层,所以我们要引入Dao层
    private UserDao userDao = null;
    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.close(connection,null,null);
        }

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

Servlet

1.编写servlet:用于获取前端请求的参数,并调用业务层判断是否存在该用户

import com.xy.pojo.User;
import com.xy.service.user.UserService;
import com.xy.service.user.UserServiceImpl;
import com.xy.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;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    //servlet控制层:调用业务层
    private UserService userService = new UserServiceImpl();
    @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");
        User user = null;
        //调用业务层相应的操作:和数据库的用户做对比

        try {
            user = userService.login(userCode,userPassword);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        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);
    }
}

2.注册servlet

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

标签:功能,userCode,登录,rs,实现,user,import,null,User
来源: https://www.cnblogs.com/xypersonal/p/16335331.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有