Session实现用户登录案例

Posted 名字真的很急用

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Session实现用户登录案例相关的知识,希望对你有一定的参考价值。

实现用户登录
1.创建一个User类,封装用户名个密码
2.提供一个首页面,欢迎用户登录,IndexServlet
3.提供一个登录的servlet:处理登录请求
4.提供一个退出的servlet:处理退出首页面的请求


第一步登录页面和封装用户数据

package session实现用户登录;

public class User {

    private String username;
    private  String password;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\\'' +
                ", password='" + password + '\\'' +
                '}';
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="/loginss">

    用户:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit">

</form>
</body>
</html>

第二步实现servlet

@WebServlet("/loginss")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username.equals("com") && password.equals("123")) {
            User user = new User(username, password);

            HttpSession session = req.getSession();
            session.setAttribute("key", user);

            //创建自己的cookie
            Cookie cookie = new Cookie("JESSIONID", session.getId());
            cookie.setMaxAge(60 * 60 * 2);
            cookie.setPath("/");
            resp.sendRedirect("/index");
        }else {
            resp.getWriter().write("用户名或密码错误!!!!!");
        }
    }
}
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("key");

        if(user==null){
            resp.sendRedirect("/login");
        }else {
            String name = user.getUsername();
            String url = "<a href='/logout'>退出</a>";
            resp.getWriter().write("欢迎你:"+name+"登录本网站"+url);
        }
    }
}
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.invalidate();
    }
}

以上是关于Session实现用户登录案例的主要内容,如果未能解决你的问题,请参考以下文章

spring-session实现session共享案例(单点登录)

13 Servlet——session案例2:用户登录主页显示用户名和注销登录

一个小的登录查看的session案例

JSP中怎样用session保存并验证用户登录信息

Session & Cookie登录案例 January 27,2020

监听器实现案例----自定义session扫描器和统计在线用户人数及用户信息