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());
     }
}
相关推荐
vx_Biye_Design2 天前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
vx_Biye_Design2 天前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
lang201509282 天前
JSR-340 :高性能Web开发新标准
java·前端·servlet
铅笔侠_小龙虾3 天前
Flutter 组件层级关系
前端·flutter·servlet
csdn2015_4 天前
Spring Boot `HttpServletRequest`
spring boot·http·servlet
GIOTTO情5 天前
舆情监测技术实战:Infoseek字节探索破解传统监测痛点
servlet
一勺菠萝丶5 天前
Jenkins 构建日志出现 `[INFO]` 乱码?原因与完整解决方案(小白必看)
java·servlet·jenkins
好好研究6 天前
SpringBoot扩展SpringMVC
java·spring boot·spring·servlet·filter·listener
gAlAxy...6 天前
SpringBoot Servlet 容器全解析:嵌入式配置与外置容器部署
spring boot·后端·servlet
Hx_Ma166 天前
SpringMVC框架提供的转发和重定向
java·开发语言·servlet