Session之验证码案例

需求分析

Session之验证码案例_第1张图片
需求分析

Session之验证码案例_第2张图片
图解需求

代码实现

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/20
  Time: 14:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>



    sessionLogin


用户名
密码
验证码
看不清换一张
<%=request.getAttribute("error_check")==null?"":request.getAttribute("error_check")%>
<%=request.getAttribute("error_user")==null?"":request.getAttribute("error_user")%>
package itcast.web.sessionDemoTest;

import javax.imageio.ImageIO;
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.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/checkCodeServletSession")
public class CheckCodeServletSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建对象  在内存中生成动态图片
        int wight = 100;
        int height = 50;
        BufferedImage bufferedImage = new BufferedImage(wight, height, BufferedImage.TYPE_INT_RGB);
        //美化图片
        //获取画笔
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.pink);
        graphics.fillRect(0,0,wight,height);
        graphics.setColor(Color.blue);
        graphics.drawRect(0,0,wight-1,height-1);
        String str="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigkhlmopqrstuvwsyz0123456789";
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i <= 4; i++) {
            int num = new Random().nextInt(str.length());
            char chr = str.charAt(num);
            stringBuffer.append(chr);
            graphics.drawString(chr+"",wight/5*i,25);
        }
        String string = stringBuffer.toString();
        HttpSession session = req.getSession();
        session.setAttribute("session_id",string);
        for (int i = 1; i <= 10; i++) {
            int num1 = new Random().nextInt(wight);
            int num2 = new Random().nextInt(wight);
            int num3 = new Random().nextInt(height);
            int num4 = new Random().nextInt(height);
            graphics.setColor(Color.GREEN);
            graphics.drawLine(num1,num3,num2,num4);
        }
        //客户端输出
       ImageIO.write(bufferedImage,"jpg",resp.getOutputStream());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
package itcast.web.sessionDemoTest;

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("/sessionLogin")
public class SessionLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String checkcode = req.getParameter("checkcode");
        HttpSession session = req.getSession();
        String session_id = (String) session.getAttribute("session_id");
        session.removeAttribute("session_id");
        if (session_id!=null&&session_id.equalsIgnoreCase(checkcode)){
            //验证码验证通过
            if ("zhangsan".equals(username)&&"123456".equals(password)){
                session.setAttribute("user",username);
                resp.sendRedirect(req.getContextPath()+"/sessionSuccess.jsp");
            }
            else {
                req.setAttribute("error_user","用户名或者密码错误,请重新输入");
                req.getRequestDispatcher("/sessionLogin.jsp").forward(req,resp);
            }
        }
        else {
            req.setAttribute("error_check","验证码错误,请重新输入");
            req.getRequestDispatcher("/sessionLogin.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/20
  Time: 15:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登陆成功


欢迎<%=request.getSession().getAttribute("user")%>登陆

你可能感兴趣的:(Session之验证码案例)