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

1. 前端

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="login" method="POST">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="submit" value="提交">
    </form>
</body>
</html>

2. 后端登录

java 复制代码
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");
   }
}

3. 后端检验页面

java 复制代码
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("<div>用户名: %s</div>", userName));
     html.append(String.format("<div>loginCount: %d</div>", loginCount));
     resp.getWriter().write(html.toString());
     }
}
相关推荐
风.foxwho8 小时前
jenkins使用 ED25519密钥 拉取Git 代码 配置
git·servlet·jenkins
Binary-Jeff3 天前
Maven 依赖作用域详解:compile、provided、runtime、test
java·spring·spring cloud·servlet·java-ee·maven
清风徐来QCQ4 天前
Servlet(Filter),Interceptor
数据库·servlet
一只大袋鼠4 天前
JavaWeb ——Cookie 对象
java·servlet·javaweb·cookie·小蛋糕
lay_liu5 天前
报错The default superclass, “jakarta.servlet.http.HttpServlet“(已经配置好tomcat)
http·servlet·tomcat
星轨初途5 天前
类和对象(中):六大默认成员函数与运算符重载全解析
开发语言·c++·经验分享·笔记·ajax·servlet
凌冰_5 天前
Servlet 过滤器(Filter)
java·servlet
我是人✓5 天前
从零入门 Servlet:JavaWeb 核心组件的实操与理解
java·servlet
不吃香菜学java6 天前
苍穹外卖-新增菜品代码开发
spring boot·spring·servlet·log4j·maven·mybatis
晨晖26 天前
Servlet的快速入门,请求和响应
hive·hadoop·servlet