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());
     }
}
相关推荐
DianSan_ERP7 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
小道仙978 天前
jenkins对接、jenkins-rest
java·servlet·jenkins·jenkins-rest
vx_Biye_Design8 天前
【关注可免费领取源码】云计算及其应用网络教学系统--毕设附源码35183
java·spring·spring cloud·servlet·eclipse·云计算·课程设计
啊哈哈哈哈哈啊哈哈10 天前
Spring MVC 项目结构学习笔记
java·spring boot·spring·servlet·maven
百锦再11 天前
Jenkins 全面精通指南:从入门到脚本大师
运维·后端·python·servlet·django·flask·jenkins
隔壁老王的代码11 天前
Jenkins的流水线详解
运维·servlet·jenkins
衍生星球11 天前
【JSP程序设计】Servlet对象 — page对象
java·开发语言·servlet·jsp·jsp程序设计
二哈喇子!11 天前
Servlet 开发技术
servlet
vx-Biye_Design11 天前
servlet家政公司管理系统-计算机毕业设计源码01438
java·vue.js·spring·servlet·tomcat·maven·mybatis
云和数据.ChenGuang11 天前
jenkins安装需要多少版本的jdk
java·servlet·jenkins