用户注册(二)------图片验证码

图片验证码可以通过img标签的src请求一个servlet生成。

Jsp页面代码片段:

<tr>
	<td>验证码</td>
	<td>
	<input type="text" name="verifyCode" style="width: 60px;"/>
	<img onclick="changeUrl(this)" style="cursor: pointer;" src="${pageContext.request.contextPath}/client/verifyCodeServlet" />
	</td>
</tr>

点击切换图片的js脚本:

<script type="text/javascript">
	function changeUrl(obj){
		obj.src = "${pageContext.request.contextPath}/client/verifyCodeServlet?t=" + new Date();
	}
</script>

生成图片的servlet代码:

//生成随机验证码
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//准备数据
		String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
		//准备随机函数
		Random random = new Random();
		int width = 60;
		int height = 30;
		//图像
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//画板
		Graphics g = image.getGraphics();
		//设置画笔颜色
		g.setColor(Color.BLACK);
		//填充矩形
		g.fillRect(0, 0, width, height);
		g.setColor(Color.WHITE);
		g.fillRect(1, 1, width-2, height-2);
		//设置字体
		g.setFont(new Font("宋体",Font.BOLD,20));
		//提供缓冲4个随机字符串
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			//设置随机颜色
			g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
			//获得一个随机字符
			int index = random.nextInt(data.length());
			String str = data.substring(index, index + 1);
			//缓冲随机字符串
			builder.append(str);
 			//绘制到画板
			g.drawString(str, width/6*(i+1), 20);
		}
		//将缓存的数据存到session中
		String bufferData = builder.toString();
		request.getSession().setAttribute("sessionVerifyCodeData", bufferData);
		//添加干扰线、点
		for(int i = 0 ; i < 5 ; 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));
		}
		//将服务器内存中的数据发送到浏览器
		ImageIO.write(image, "jpg", response.getOutputStream());
		
	}

验证提交的图片验证码的servlet的代码:

//或得用户填写的验证码
		String verifyCode = request.getParameter("verifyCode");
		//或得session中的验证码
		String sessionVerifyCodeData = (String) request.getSession().getAttribute("sessionVerifyCodeData");
		//判断服务器是否有数据
		if(sessionVerifyCodeData != null){
			// 将session中缓存的数据移除,保证一次性
			request.getSession().removeAttribute("sessionVerifyCodeData");
			if(! sessionVerifyCodeData.equalsIgnoreCase(verifyCode)){
				//不匹配
				request.setAttribute("message", "验证码有误,请重写填写");
				request.getRequestDispatcher("/client/register.jsp").forward(request, response);
				return;
			}
		}else{
			// 重复提交
			request.setAttribute("message", "不要重复提交");
			request.getRequestDispatcher("/client/register.jsp").forward(request, response);
			return;
		}

页面效果:
这里写图片描述

Coding Diary

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值