JAVA实现验证码登陆

 

实现效果:

package session;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
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;

public class ImageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        创建图片
        int width = 80;
        int height = 40;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//        创建图层获得画板
        Graphics g = image.getGraphics();
//        确认画笔颜色
        g.setColor(Color.BLACK);
        //填充矩形
        g.fillRect(0,0,width-2,height-2);
        String dataString="ABCDEFGHIJHLMNOPQRSTUVWXYZabcdefghijklmnopqlstuvwxyz1234567890";
        //设置字体
        g.setFont(new Font("宋体",Font.BOLD,30));
        //缓存随机生成的字符
        StringBuffer buf = new StringBuffer();
        Random random = new Random();

        //截取字符
        for(int i=0;i<4;i++){
            //设置字体颜色  随机
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            //获得一个随机字符
            int index = random.nextInt(62);
            String str = dataString.substring(index,index+1);
            //加入画板
            g.drawString(str,20*i,30);
            buf.append(str);
        }


        //干扰线
        for(int i=0;i<10;i++){
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            g.drawLine(random.nextInt(width),random.nextInt(height),random.nextInt(width),random.nextInt(height)) ;
        }

        
        HttpSession session = req.getSession();
        session.setAttribute("cap",buf.toString());

        //设置响应类型
        resp.setContentType("image/jpeg");
        //将图片发送给浏览器
        ImageIO.write(image,"jpg",resp.getOutputStream());

    }

}

LoginServlet

package session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        PrintWriter writer = resp.getWriter();

        String imageNum = req.getParameter("imagenum");
        if (imageNum != null && imageNum!=""){
            HttpSession session = req.getSession();
            String cap = session.getAttribute("cap").toString();

            if(cap.equalsIgnoreCase(imageNum)){
                writer.write("验证通过!");
            }else{
                writer.write("验证码错误!");
            }
            //无论什么情况验证码只能用一次
            session.removeAttribute("cap");
        }else {
            writer.write("请填写参数");
        }
    }
}

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="imagenum"><img src="/image">
    <input type="submit" value="提交">
</form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值