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. JMS开源比较

    Java开源JMS消息中间件 mom4j mom4j是一个完全实现JMS1.1规范的消息中间件并且向下兼容JMS1.0与1.02.它提供了自己的消息处理存储使它独立于关系数据与语言,所以它的客户端可以 ...

  2. 基于visual Studio2013解决C语言竞赛题之0608水仙花函数

     题目 解决代码及点评 /* 功能:写一函数判断某数是否"水仙花数",所谓"水仙花数"是指一个三位数, 其各位数字立方和等于该数本身. */ #includ ...

  3. JavaScript对滚动栏的操作

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. iOS苹果官方Demo合集

    Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...

  5. Python 3语法小记(四)字典 dictionary

    字典是Python里面一种无序存储结构,存储的是键值对 key - value.关键字应该为不可变类型,如字符串.整数.包含不可变对象的元组. 字典的创建很简单,用 d = {key1 : value ...

  6. PHP - 字符串操作

    第8章 字符串处理 学习要点: 1.字符串格式化 2.操作子字符串 3.字符串比较 4.查找替换字符串 5.处理中文字符 在每天的编程工作中,处理.调整以至最后控制字符串是很重要的一部分,一般也认为这 ...

  7. 基于visual Studio2013解决C语言竞赛题之1025Bessel函数

          题目 解决代码及点评 /* 功能:25. Bessel函数Jn(X)有以下的递推关系: J[n+1](x)=(2n+1)/x*J[n](x)-J[n-1](x) 并 ...

  8. VB.net总结

    .NET视频差点儿相同用时一周结束,总体感觉就是"走耳不走脑".刚開始看视频理解起来有一点困难,台湾口音以及台湾与大陆计算机术语的差异,让我把前几集相当于直接忽略过了(建议打算看这 ...

  9. 跨平台编程中的宏定义(__LINE__和__DATE__极其有用)

    要用师兄的计算机算东西,无赖那上面是WINDOWS的系统,为了写出能够跨平台的代码,需要在代码中用到宏来选择编译.一种方法是自己在Makefile里面定义好该平台对应的宏.实际上,编译器基本上都会有一 ...

  10. post 请求参数

    perl代码: my $login_url='http://192.168.1.1/getpage.gch?pid=1001&logout=1'; my $res = $ua->post ...