欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Session实战保存验证码

程序员文章站 2022-05-04 14:24:01
创建验证码导入ValidateCode.jar()创建生成验证码的ServletCreateCodeController.javapackage com.newer.servletProject.servlet.controller;import cn.dsna.util.images.ValidateCode;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;i...

创建验证码

  • 导入ValidateCode.jar()
  • 创建生成验证码的Servlet

CreateCodeController.java

package com.newer.servletProject.servlet.controller;

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;

@WebServlet(name = "CreateCodeController",value = "/createCode")
public class CreateCodeController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建验证码图片
        ValidateCode code=new ValidateCode(200,30,4,20);
        String codes=code.getCode();
        //将codes存进session中
        HttpSession session=request.getSession();
        session.setAttribute("codes",codes);
        //验证码图片响应给客户端
        code.write(response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

 loginMgr.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理员登录</title>
</head>
<body>
    <form action="/day01web1_war_exploded/loginMgr" method="post">
        用户名:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        验证码:<input type="text" name="inputVcode"/><img src="/day01web1_war_exploded/createCode"/><br/>
        <input type="submit" value="登录"/>

    </form>
</body>
</html>

LoginMgrController.java

package com.newer.servletProject.servlet.controller;

import com.newer.servletProject.entity.Manager;
import com.newer.servletProject.service.ManagerService;
import com.newer.servletProject.service.impl.ManagerServiceImpl;

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;

@WebServlet(name = "LoginMgrController",value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.处理乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //2.收参
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String inputVcode=request.getParameter("inputVcode");

        //从request对象中获取codes
        String codes=(String)request.getSession().getAttribute("codes");
        //判断验证码
        if(!inputVcode.isEmpty()&&inputVcode.equalsIgnoreCase(codes)){
            //3.调用业务方法
            ManagerService managerService=new ManagerServiceImpl();
            Manager mgr=managerService.login(username,password);

            //4.处理结果,流程跳转
            if(mgr!=null){
                //登录成功

                //将管理员信息存储在session中
                HttpSession session=request.getSession();
                session.setAttribute("mgr",mgr);

                //跳转,目标,方式
                response.sendRedirect("/day01web1_war_exploded/showallController");

            }else {
                //登录失败
                response.sendRedirect("/day01web1_war_exploded/loginMgr.html");
            }
        }else {
            response.sendRedirect("/day01web1_war_exploded/loginMgr.html");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

 

本文地址:https://blog.csdn.net/weixin_44364444/article/details/109555726

相关标签: Servlet