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. Thread.sleep还是TimeUnit.SECONDS.sleep

    转http://stevex.blog.51cto.com/4300375/1285767/ 刚看到TimeUnit.SECONDS.sleep()方法时觉得挺奇怪的,这里怎么也提供sleep方法? ...

  2. HEVC码率控制浅析——HM代码阅读之一

    HM的码率控制提案主要参考如下三篇:K0103,M0036,M0257.本文及后续文章将基于HM12.0进行讨论,且首先仅讨论K0103对应的代码,之后再陆续补充M0036,M0257对应的代码分析, ...

  3. Java Arrays Tutorial (3)

    Java Arrays Tutorial (3) Data types have a specific set of values. A byte cannot hold a value larger ...

  4. IOS开发之UILabel动态高度设置方法

    项目中有这样的需求,要显示一本书的概述,默认显示2行,点击展开按钮,显示全部,点击收回,有显示2行. 开始的时候按钮事件中,可能写的是这样一段代码: if (isExpand) { [lblBrief ...

  5. Ember.js - About

    Ember.js - About More Productive Out of the Box.   Write dramatically less code with Ember's Handleb ...

  6. PHP高级编程SPL

    这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去非常复杂,可是非常实用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 因为这是供自己參考的笔记,不 ...

  7. 【linux】 Makefile之make menuconfig /uImage

      欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http: ...

  8. 一个问题:关于类型转换Type Cast(汇编讲解 as 语法)

    问题如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34   ...

  9. 基于visual Studio2013解决面试题之0410计算二进制中1的个数

     题目

  10. 浅析嵌入式Linux系统的构成和启动过程

    在我们的周围,大量的嵌入式设备都是基于Linux系统来构建的,嵌入式Linux与主机Linux相比有着自己的一些特点,本文就嵌入式Linux系统的构成和启动过程做一些总结. 一.嵌入式Linux系统构 ...