【javaweb】使用java制作简单的验证码

在这里我们需要用到java的画笔工具,所以我们需要导入以下包

import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*"


然后我就使用java脚本来实现一个小小的验证码

<%@ page contentType="image/jpeg; charset=utf-8" 
		language="java" import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*"
		pageEncoding="UTF-8"%>
		<!-- 以上导入awt和awt.image包 -->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>验证码</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
  		<%!
  			//获取随机颜色
  			public Color getColor(){
  				Random random = new Random();
  				//使用rgb()随机产生颜色
  				int r = random.nextInt(256);
  				int g = random.nextInt(256);
  				int b = random.nextInt(256);
  				
  				return new Color(r,g,b);
  			}
  			
  			//获取随机数字  产生一个4位数
  			public String getNum(){
  				String str = "";
  				Random random = new Random();
  				for(int i = 0;i < 4;i++){
  					str += random.nextInt(10); //0-9
  				}
  				return str;
  			}
  		 %>
  		 
  		 <%
  		 	/* 清除缓存 */
  		 	response.setHeader("pragma", "mo-cache");
			response.setHeader("cache-control", "no-cache");
			response.setDateHeader("expires", 0);
			//产生矩形框
			BufferedImage image = new BufferedImage(80,30,BufferedImage.TYPE_INT_RGB);
			//获取画笔工具
			Graphics g = image.getGraphics();
			//设置矩形框的颜色
			g.setColor(new Color(200,200,200));
			//设置坐标和宽高
			g.fillRect(0, 0, 80, 30);
  		 	
  		 	//随机产生干扰线
  		 	for(int i = 0;i < 30;i++){
  		 		Random random = new Random();
  		 		int x = random.nextInt(80);
  		 		int y = random.nextInt(30);
  		 		int x1 = random.nextInt(x + 10);
  		 		int y1 = random.nextInt(y + 10);
  		 		//设置随机颜色
  		 		g.setColor(getColor());
  		 		//画出来
  		 		g.drawLine(x, y, x1, y1);
  		 	}
  		 	
  		 	//字的颜色和数字
  		 	g.setFont(new Font("Microsoft YaHei",Font.BOLD,16));
  		 	g.setColor(Color.BLACK);
  		 	//获取随机数字
  		 	String checkNum = getNum();
  		 	
  		 	//给字拼接空格
  		 	StringBuffer sb = new StringBuffer();
  		 	for(int i = 0;i < checkNum.length();i++){
  		 		sb.append(checkNum.charAt(i) + " ");
  		 	}
  		 	//画出数字
  		 	g.drawString(sb.toString(), 15, 20);
  		 	//存入session域中
  		 	session.setAttribute("CHECKNUM", checkNum); //例如1010
  		 	//将图像以jpeg的形式通过字节流输出
  		 	ImageIO.write(image, "jpeg", response.getOutputStream());
  		 	//清除缓存
  		 	out.clear();
  		 	//放入body中
  		 	out = pageContext.pushBody();
  		  %>
  		  
  </body>
</html>

结果如下:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值