基于Servlet实现简单系统登录

一、需求

1.实现分层架构:bean dao service controller层
2.基于Servlet实现controller:登录成功进入欢迎页面;登录失败重新登录,并显示上次的登录用户名
3.前端:登录页面login.jsp 欢迎页面success.jsp

二、项目结构

bean层

User.java

package bean;

public class User {
    private int id;
    private String pwd;
    private String nickname;
    private String addr;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
    public User(int id, String pwd, String nickname, String addr) {
        super();
        this.id = id;
        this.pwd = pwd;
        this.nickname = nickname;
        this.addr = addr;
    }
    public User() {
        super();        
    }
    
}
View Code

dao层

UserDAO.java

package dao;

import bean.User;

public interface UserDAO {
    User selectUserById(int id);
}
View Code

UserDaoImpl.java

package dao;

import bean.User;

public class UserDaoImpl implements UserDAO {

    @Override
    public User selectUserById(int id) {
        if(id==1) {
            return new User(1,"123456","张三","山东烟台市莱山区");
        } else {
            return null;
        }    
        
    }
}
View Code

 

service层

UserService.java

package service;

import bean.User;

public interface UserService {
    boolean login(User user);
}
View Code

UserServiceImpl.java

package service;

import bean.User;
import dao.UserDAO;

public class UserServiceImpl implements UserService{
    private UserDAO dao;
    public UserServiceImpl(UserDAO dao) {
        this.dao = dao;
    }
    @Override
    public boolean login(User user) {
        User userDB=dao.selectUserById(user.getId());        
        if(userDB!=null && userDB.getPwd().contentEquals(user.getPwd())) {
            user.setNickname(userDB.getNickname());
            user.setAddr(userDB.getAddr());
            return true;
        }else
            return false;
    }

}
View Code

 

controller层

checkLogin.java

检测登录情况

package controller;

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

import bean.User;
import dao.UserDaoImpl;
import service.UserService;
import service.UserServiceImpl;

/**
 * Servlet implementation class checkLogin
 */
@WebServlet("/checkLogin")
public class checkLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public checkLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1处理请求参数
        //2调用service进行处理
        //3根据处理结果,重定向转发到Servlet jsp
        request.setCharacterEncoding("utf-8"); 
        String id=request.getParameter("name");
        String pwd = request.getParameter("pwd");
        UserService service = new UserServiceImpl(new UserDaoImpl());
        User user=new User();
        user.setId(Integer.parseInt(id));
        user.setPwd(pwd);
        if(service.login(user))//密码验证 登录成功进入欢迎页面
        {
            request.setAttribute("user",user.getNickname());
            request.getRequestDispatcher("\\success.jsp").forward(request, response);
        }
        else//登录失败重新登录,并显示上次的登录用户名
        {
            request.setAttribute("name",user.getNickname());
            request.getRequestDispatcher("\\login.jsp").forward(request, response);
        }
    }

}

 

前端

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form method="post" action=checkLogin>
    <table>
        <tr>
            <td>输入用户名:</td>
            <td><input type="text" name="name" value=<%=request.getAttribute("user") %>>
            </td>
        </tr>
        <tr>
            <td>输入密码:</td>
            <td><input type="password" name="pwd" value=<%=request.getParameter("pwd")%>>
            </td>
        </tr>
        <tr >
            <td colspan=2><input type="submit" value="登录"></td>
        </tr>
    </table>
</form>
</body>
</html>

 

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*,bean.*"%>>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎页面</title>
</head>
<body>
    <h1>登录成功</h1>
    <h2>
        欢迎您!<%=request.getAttribute("user")%>
    </h2>
</body>
</html>

 

posted @ 2020-04-16 08:09  王陸  阅读(1443)  评论(0编辑  收藏  举报