• servlet实现登录功能【当用户当前未登陆,跳转登录页面才能访问,若已经登录了,才可以直接访问】


    1. 前端

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
        <form action="login" method="POST">
            <input type="text" name="username">
            <input type="password" name="password">
            <input type="submit" value="提交">
        form>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2. 后端登录

    package EnableUserLogin;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpSession;
    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 java.io.IOException;
    @WebServlet("/login")
    public class LoginServlet extends HttpServlet {
         @Override
         protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
               resp.setContentType("text/html; charset=utf-8");
               // 1. 获取到用户提交的用户名和密码
               String username = req.getParameter("username");
               String password = req.getParameter("password");
               // 2. 判定用户名密码是否正确
               if (!username.equals("admin") || !password.equals("123")) {
                     // 登陆失败
                     resp.getWriter().write("登陆失败");
                     return;
             }
               // 登陆成功
               System.out.println("登陆成功");
               // 设置 Session
               HttpSession session = req.getSession(true);
               session.setAttribute("username", "admin");
               session.setAttribute("loginCount", "0");
               resp.sendRedirect("index");
       }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    3. 后端检验页面

    package EnableUserLogin;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpSession;
    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 java.io.IOException;
    
    
    @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");
         // 1. 判定当前用户是否已经登陆
         HttpSession session = req.getSession(false);
         if (session == null) {
             // 用户没有登陆, 重定向到 login.html
             resp.sendRedirect("login.html");
             return;
          }
         // 2. 如果已经登陆, 则从 Session 中取出访问次数数据
         String userName = (String)session.getAttribute("username");
         String countString = (String)session.getAttribute("loginCount");
         int loginCount = Integer.parseInt(countString);
         loginCount += 1;
         session.setAttribute("loginCount", loginCount + "");
         // 3. 展示到页面上.
         StringBuilder html = new StringBuilder();
         html.append(String.format("
    用户名: %s
    "
    , userName)); html.append(String.format("
    loginCount: %d
    "
    , loginCount)); resp.getWriter().write(html.toString()); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    软件测试的调用接口怎么调用,逻辑是什么?
    入行IC | 新人入行IC选择哪个岗位更好?
    【JavaScript高级】函数相关知识:函数、纯函数、柯里化、严格模式
    CUDA C编程权威指南:2.1-CUDA编程模型
    面试中经常问到的几个问题,快来看看能答对几道吧
    Qgis 开发初级 《ToolBox》
    HAProxy的详解和使用
    Java项目:ssm房屋租赁管理系统
    为什么多级页表比单级页表更节省内存?
    常规业务如何做到幂等性
  • 原文地址:https://blog.csdn.net/m0_63571404/article/details/132919392