checkNum.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,javax.imageio.*"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%!
Color  getRandColor(int fc,int bc)//给定范围随机选颜色
{
Random random = new Random();
if(fc>255) fc= 255;
if(bc>255) bc= 255;
int r= fc+random.nextInt(bc-fc);
int g= fc+random.nextInt(bc-fc);
int b= fc+random.nextInt(bc-fc);
 
return new Color(r,g,b);
}
%>
<%
response.setHeader("Pragma","No-cache");//设置页面不缓冲
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
int width=60,height=20;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
 
Graphics g = image.getGraphics();//获取图像上下文
Random random = new Random();//生成随机对象
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));//设置字体
for(int i=0;i<155;i++)
{
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x+x1,y+y1);
 
}
//随机产生验证码
String sRand = "";
for(int i=0;i<4;i++)
{
String rand = String.valueOf(random.nextInt(10));
sRand+=rand;
//将验证码显示到图像中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
//设置显示随机数的颜色
g.drawString(rand,13*i+6,16);
}
//将验证码存放到session中
session.setAttribute("rand",sRand);
//图像生效
g.dispose();
//输出图像到页面
ImageIO.write(image,"JPEG",response.getOutputStream());
out.clear();
out = pageContext.pushBody();

%>

可以在登录login.jsp下直接通过:   这种格式调用; 
                验证码:<input class="imgbutton"  name="checknum" type="text" size="15" id ="checknum" maxlength="4"/>
   
 
<img src="checknum.jsp" alt="Change" border="1" onclick="changeCheckNum()"/><br/>
   
 
<input type="submit" name="submit" value="login"/>

如何用jsp页面生成随机的验证数字码的更多相关文章

  1. 使用JSP页面生成PDF报表

    转自:http://developer.51cto.com/art/200907/134261.htm 1.iText简介 iText是一个开放源码的Java类库,可以用来方便地生成PDF文件.大家通 ...

  2. PHP中生成随机字符串,数字+大小写字母随机组合

    简单的生成随机字符串: /* * 生成随机字符串 * * $length 字符串长度 */ function random_str($length) { // 密码字符集,可任意添加你需要的字符 $c ...

  3. JSP页面生成验证码功能

    <%@ page language="java" contentType="text/html; charset=UTF-8" import=" ...

  4. c# 生成随机N位数字串(每位可以重复)

    /// <summary> /// 生成随机数字窜 /// </summary> /// <param name="Digit">位数</ ...

  5. c# 生成随机N位数字串(每位都不重复)

    /// <summary> /// 生成随机数字窜 /// </summary> /// <param name="Digit">位数</ ...

  6. php 生成随机字符串,数字,大写字母,小写字母,特殊字符可以随意组合

    * 生成随机字符串* @param int       $length  要生成的随机字符串长度* @param string    $type    随机码类型:0,数字+大小写字母:1,数字:2, ...

  7. Java生成随机不反复推广码邀请码

    欢迎进入我的博客:blog.scarlettbai.com查看很多其它文章 近期接到一个需求.要批量生成推广码,首先我们知道推广码的特效有例如以下两点: 1:不可反复 2:不能够被猜測出 关于这两点, ...

  8. jsp 页面导出excel时字符串数字变成科学计数法的解决方法

    web导出excel数据格式化 原文地址:http://www.cnblogs.com/myaspnet/archive/2011/05/06/2038490.html   当我们把web页面上的数据 ...

  9. C#生成随机字符串(数字,字母,特殊符号)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

随机推荐

  1. iOS开发之使用Ad Hoc进行测试

    由于最近某个项目需要给别人测试,使用的是Ad Hoc方法 首先登录开发者官网配置证书 1.添加Certificates,从电脑获取certSigningRequest然后添加进去 2.在Identif ...

  2. JS 2016-09-30T22:04:27.5220743+08:00 转换为日期

    1.转换代码 new Date(item.CreatedDate).Format("yyyy-MM-dd hh:mm") 2.需要拓展的方法 // 对Date的扩展,将 Date ...

  3. 经典集合 与 IQueryable集合 的差别

    经典集合 与 IQueryable集合 的差别 经典集合与IQueryable 集合存在本质的区别,经典结合是在内存中开辟一片区域用来存储数据,而IQueryable集合是延迟加载的集合,只有在用到的 ...

  4. JAVA编程相关:eclipse如何导入已有工程

    eclipse使用过程中,经常会遇到导入外部eclispe工程的情况,导入外部eclipse也就是将已有的eclipse工程导入到eclipse中,那么如何导入外部工程呢?下面为大家分享导入已有ecl ...

  5. 使用Boost.PropertyTree处理XML、JSON和INI数据

    Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的.目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0. 主要作用/应用场合 B ...

  6. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  7. leetcode——Search a 2D Matrix 二维有序数组查找(AC)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. 学习算法-基数排序(radix sort)卡片分类(card sort) C++数组实现

    基数排序称为卡片分类,这是一个比较早的时间越多,排名方法. 现代计算机出现之前,它已被用于排序老式打孔卡. 说下基数排序的思想.前面我有写一个桶式排序,基数排序的思想是桶式排序的推广. 桶式排序:ht ...

  9. C++要点

           以下的这些要点是对全部的C++程序猿都适用的.我之所以说它们是最重要的,是由于这些要点中提到的是你通常在C++书中或站点上无法找到的.如:指向成员的指针,这是很多资料中都不愿提到的地方, ...

  10. MSSQL - 多表查询

    SELECT u.UserNumber, u.UserName, c.CarNumber, c.CarName, c.CarEngine, s.BuyLs, s.BuyTime FROM Tb_Sal ...