使用session和验证码实现登录并解决浏览器禁用cookie问题

导包:
在这里插入图片描述

login.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
    <body>
        <form action="/LoginServlet" method="post">
            用户名:
            <input type="text" name="username"><br>&nbsp;码:
            <input type="password" name="password"><br>
            验证码:
            <input type="text" name="code"><img src="/ValidateCodeServlet"><br>
            <button type="submit">登录</button>
        </form>
    </body>
</html>

welcome.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
    <body>
        welcome!
        <br>
        <a href="/login.jsp">退出</a>
    </body>
</html>

ValidateCodeServlet.java

package com.qfedu.servlet.demo;

import cn.dsna.util.images.ValidateCode;

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 javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author xue_yun_xiang
 * @create 2021-04-13-19:42
 */
@WebServlet("/ValidateCodeServlet")
public class ValidateCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //验证码工具类
        ValidateCode validateCode = new ValidateCode(200, 30, 4, 30);

        //获取生成的验证码
        String code = validateCode.getCode();
        System.out.println(code);

        //将验证码放入会话中
        HttpSession session = req.getSession();
        session.setAttribute("code",code);


        //将验证码图片放到前端并将图片写入响应输出流
        validateCode.write(resp.getOutputStream());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

LoginServlet.java

package com.qfedu.servlet.demo;

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 javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author xue_yun_xiang
 * @create 2021-04-13-19:32
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");

        HttpSession session = req.getSession();

        //获取生成的验证码
        String sessionCode = (String) session.getAttribute("code");

        //验证码忽略大小写
        if ("root".equals(username) && "123456".equals(password) && code.equalsIgnoreCase(sessionCode)) {

            //设置session的成功标记
            session.setAttribute("success",true);

            //重定向
            resp.sendRedirect("/WelcomeServlet");

            //禁用cookie后的解决办法:重写URL
            //重定向:302
            // String url = resp.encodeURL("/WelcomeServlet");
            //resp.sendRedirect(url);
    




        } else {
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

WelcomeServlet.java

package com.qfedu.servlet.demo;

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 javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author xue_yun_xiang
 * @create 2021-04-13-19:40
 */
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();

        Object result = session.getAttribute("success");

        if (result != null) {
            boolean success = (boolean)result;

            if (success) {
                //重定向
                resp.sendRedirect("/welcome.jsp");
            } else {
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

LoginOutServlet.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xue_yun_xiang
 * @create 2021-04-13-19:39
 */
@WebServlet("/LoginOutServlet")
public class LoginOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值