javaWeb实现图片验证码功能

16 篇文章 0 订阅

java程序实现画图

public static void main(String[] args)throws Exception {
		String message="3456789abcdefghjkmnpqstuvwxy";
		//通过java生成图片
		//现实:画画
		//1 准备画板-
		//2 铺上画布
		//3 准备笔
		//4 画背景
		//5 画主题
		//6 撤画布
		//1 在内存中开辟空间 用于画画
		BufferedImage bin=new BufferedImage(200, 50, BufferedImage.TYPE_INT_RGB);
		//2 获取画笔
		Graphics2D g=(Graphics2D)bin.getGraphics();
		//3 涂染料 画背景
		g.setColor(Color.WHITE);
		//4 画背景:长方形
		g.fillRect(2, 2, 195, 45);
		//5 画主题
		//随机四五个字符
		String str="";
		for (int i = 0; i <4; i++) {
			  str+=message.charAt((int)(Math.random()*message.length()));
		}
		//给字符设置文字样式
		Font font=new Font(null, Font.BOLD, 34);
		g.setFont(font);
		//把字符串中的所有字符画到画布上
		for (int i = 0; i < str.length(); i++) {
			//笔蘸墨
			g.setColor(new Color((int)(Math.random()*100+120), (int)(Math.random()*100+120), (int)(Math.random()*100+120)));
			//写字
			g.drawString(str.charAt(i)+"", i*45+10, 40);
		}
		//画干扰线
		for (int i = 0; i < (int)(Math.random()*5+5); i++) {
			g.setColor(new Color((int)(Math.random()*50+120), (int)(Math.random()*50+120), (int)(Math.random()*50+120)));
			g.drawLine(2, (int)(Math.random()*43+2), 195, (int)(Math.random()*43+2));
		}
		//6 创建流与目的文件关联
		File muDiFile=new File("E:\\imgs\\"+(int)(Math.random()*100+120)+".jpg");
		FileOutputStream fout=new FileOutputStream(muDiFile);
		//把内存中BufferedImage中的信息通过输出流写道目的文件中
		ImageIO.write(bin, "JPEG", fout);
		fout.close();
	}

web实现验证码

页面的img的src请求servlet生成验证码图片

servlet需要把验证码保存到session中用于登录判断输入的验证码是否正确

比较session域中的验证码和用户输入的验证码,如果相同则登录成功

login.jsp

<h1>用户登录表单</h1>
<c:if test="${not empty requestScope.message}">
    <h3>${requestScope.message}</h3>
</c:if>
<form method="get" action="<c:url value='/user/login'/>">
    <table>
        <tr>
            <th>用户名称:</th>
            <td><input type="text" name="uname"/></td>
        </tr>
        <tr>
            <th>用户密码:</th>
            <td><input type="password" name="upwd"/></td>
        </tr>
        <tr>
            <th><img src="<c:url value='/user/yzm'/>" id="img_yzm"/></th>
            <td><input type="text" name="uyzm"/></td>
        </tr>
        <tr>
            <th colspan="2"><INPUT type="reset" value="重置"/>&nbsp;&nbsp;&nbsp;&nbsp;
                <INPUT type="submit" value="登录"/></th>
        </tr>
    </table>
</form>

<!-- 添加js事件  点击图片 图片更新 -->
<script type="text/javascript">
    //文档加载完毕 给img_yzm注册点击事件
    window.onload=function(){
        //获取img_yzm
        var oimg=document.getElementById("img_yzm");
        oimg.onclick=function(){
            //请求时 为了不使用缓存  添加一个无用的参数  参数值每次不同即可
            this.src="<c:url value='/user/yzm?n='/>"+Math.random();
        }
    }
</script>

servlet:生成验证码

String message="3456789abcdefghjkmnpqstuvwxy";
//1 在内存中开辟空间 用于画画
BufferedImage bin=new BufferedImage(200, 50, BufferedImage.TYPE_INT_RGB);
//2 获取画笔
Graphics2D g=(Graphics2D)bin.getGraphics();
//3 涂染料 画背景
g.setColor(Color.WHITE);
//4 画背景:长方形
g.fillRect(2, 2, 195, 45);
//5 画主题
//随机四五个字符
String str="";
for (int i = 0; i <4; i++) {
    str+=message.charAt((int)(Math.random()*message.length()));
}
//给字符设置文字样式
Font font=new Font(null, Font.BOLD, 34);
g.setFont(font);
//把字符串中的所有字符画到画布上
for (int i = 0; i < str.length(); i++) {
    //笔蘸墨
    g.setColor(new Color((int)(Math.random()*100+120), (int)(Math.random()*100+120), (int)(Math.random()*100+120)));
    //写字
    g.drawString(str.charAt(i)+"", i*45+10, 40);
}
//画干扰线
for (int i = 0; i < (int)(Math.random()*5+5); i++) {
    g.setColor(new Color((int)(Math.random()*50+120), (int)(Math.random()*50+120), (int)(Math.random()*50+120)));
    g.drawLine(2, (int)(Math.random()*43+2), 195, (int)(Math.random()*43+2));
}
//把验证码的信息装入session
request.getSession().setAttribute("yzm", str);
//把内存中图片的信息通过response的输出流 响应给客户端
ImageIO.write(bin, "JPEG", response.getOutputStream());

servlet:判断登录

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取请求参数
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");
String uyzm=request.getParameter("uyzm");
String message="";
//判断参数的正确
if(!uname.equals("韩梅梅")){
    message="用户名错误!";
}else if(!upwd.equals("123")){
    message="用户密码错误!";
}else {
    String yzm=request.getSession().getAttribute("yzm")+"";
    if(!uyzm.equals(yzm)){
        message="验证码错误!";
    }
}
//如果message有值 登录失败 回到登录页面
if(!message.isEmpty()){
    request.setAttribute("message", message);
    request.getRequestDispatcher("/login.jsp").forward(request, response);
    return;
}
response.getWriter().print("<font color='red'>"+uname+"登录成功!</font>");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值