Session实现验证码登录

这里写图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="/SessionTest/Cservlet" >
用户名 : <input type="text" name="username"/><br>
密码 :  <input type="password" name="password"/><br>
验证码 : <input type="text" name="checkCode" size="4"/><font color="red" >${requestScope.error}</font><br>
<img  src="/SessionTest/Aservlet"/><br>
<input type="submit" value="登录"/><br>
</form>

</body>
</html>

public class Aservlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //添加响应正文要指定类型

        response.setContentType(getServletContext().getMimeType("xxx.jpeg"));
        //获得验证码
        ValidateCode code=new ValidateCode(150, 40, 4, 2);
        //验证码放入session
        request.getSession().setAttribute("checkCode", code.getCode());
        System.out.print(code.getCode());
        //返回验证码输出流
        code.write(response.getOutputStream());



    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}




@WebServlet("/Cservlet")
public class Cservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Cservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("cccccccc");
        String userCheckCode =  request.getParameter("checkCode");
        String checkCode =(String) request.getSession().getAttribute("checkCode");
        if (userCheckCode!=null&&userCheckCode.equalsIgnoreCase(checkCode)) {
            System.out.print("登录成功");
        }else {
            request.setAttribute("error", "验证码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);;
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值