jsp制作验证码

RandomPic.Java

---------------------------------
package  et.command.pic;

import  java.awt.Color;
import  java.awt.Font;
import  java.awt.Graphics;
import  java.awt.image.BufferedImage;
import  java.util.Random;

public  class  RandomPic
{
private  final  int  width=60;//背景(图片)的宽
private  final  int  height=20;//背景(图片)的高

private  final  int  interfereLineNum  88;//干扰线个数
private  final  int  interferePointNum  88;//干扰线个数
private  final  int  interfereLineLenMax  12;//干扰线随机长度最大值
private  final  int  authenticationCodeNumber   4;//认证码的位数
//背景色的范围
private  final  int  backgroundColorBegin  =200;
private  final  int  backgroundColorFinal  =250;

private  final  int  interfereColorBegin  =160;
private  final  int  interfereColorFinal  =200;

private  final  int  radomNumberColorBegin  =20;
private  final  int  radomNumberColorFinal  =130;
//字体
private  final  String  fontName="Times  New  Roman";
private  final  int  fontStyle  Font.PLAIN;
private  final  int  fontSize  18;

private  final  int  stringWidth  =13;//string宽度坐标
private  final  int  stringHight  =16;//string高度坐标
private  final  int  stringClearance  =6;//string间距
//rgb范围
private  final  int  rgbValue  255;
//
private  Graphics  graphics;//内存中虚拟的画布

public  String  authenticationCode=new  String();//取随机产生的认证码(4位数字),对外的接口
public  BufferedImage  image;//图片的内存,对外的接口

//认证码由服务器生成
public  RandomPic(){
init();
}
//认证码客户端生成,rn为客户端生成的认证码
public  RandomPic(String  rn){
init();
authenticationCode  rn;
}

private  void  init(){
image  new  BufferedImage(width,  height,  BufferedImage.TYPE_INT_RGB);
graphics  image.getGraphics();
//System.out.println("hello");
}
public  void  drawServer(){
//画基本框架
drawBasic();
//画干扰线或者干扰点
drawInterfereLine();
//drawInterferePoint();
//画随机数
drawRadomNumberServer();
//图象生效
graphics.dispose();
}
public  void  drawClient(){
//画基本框架
drawBasic();
//画干扰线或者干扰点
drawInterfereLine();
//drawInterferePoint();
//画随机数
drawRadomNumberClient();
//图象生效
graphics.dispose();
}
//  给定范围获得随机颜色bc:begin  color  ,fc:finally  color
private  Color  getRandColor(int  fc,int  bc){
Random  random  new  Random();
if(fc>rgbValue)  fc=rgbValue;
if(bc>rgbValue)  bc=rgbValue;
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);
}

//设定图片基本信息
private  void  drawBasic(){
//设定背景色
graphics.setColor(getRandColor(backgroundColorBegin,backgroundColorFinal));
graphics.fillRect(0,  0,  width,  height);
//设定字体
graphics.setFont(new  Font(fontName,fontStyle,fontSize));
//画边框
graphics.setColor(new  Color(rgbValue,rgbValue,rgbValue));
graphics.drawRect(0,0,width-1,height-1);
}
//随机产生interfereLine条干扰线,使图象中的认证码不易被其它程序探测到
private  void  drawInterfereLine(){
Random  random  new  Random();
graphics.setColor(getRandColor(interfereColorBegin,interfereColorFinal));
for  (int  i=0;i<interfereLineNum;i++){
int  random.nextInt(width);
int  random.nextInt(height);
int  xl  random.nextInt(interfereLineLenMax);
int  yl  random.nextInt(interfereLineLenMax);
graphics.drawLine(x,y,x+xl,y+yl);
}
}
//  随机产生interfereLine条干扰点,使图象中的认证码不易被其它程序探测到
@SuppressWarnings("unused")
private  void  drawInterferePoint(){
Random  random  new  Random();
graphics.setColor(getRandColor(interfereColorBegin,interfereColorFinal));
for  (int  i=0;i<interferePointNum;i++){
int  random.nextInt(width);
int  random.nextInt(height);
graphics.drawLine(x,y,x,y);
}
}
//  取随机产生的认证码(4位数字),由服务器端生成
private  void  drawRadomNumberServer(){
Random  random  new  Random();
for  (int  i=0;i<authenticationCodeNumber ;i++){
String  rand=String.valueOf(random.nextInt(10));
authenticationCode+=rand;
graphics.setColor(getRandColor(radomNumberColorBegin,radomNumberColorFinal));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
graphics.drawString(rand,stringWidth*i+stringClearance,stringHight);//后两个参数为坐标
}
//System.out.println(authenticationCode);
}
//取随机产生的认证码(4位数字),由客户端生成
//<script>
//document.write("<img  border=0  src=image.jsp?rand="+Math.random()*10000+">");
//</script>
  private  void  drawRadomNumberClient(){
authenticationCode  authenticationCode.substring(0,authenticationCode.indexOf("."));
switch(authenticationCode.length()){
      case  1:  authenticationCode  "000"+authenticationCode;  break;
      case  2:  authenticationCode  "00"+authenticationCode;  break;
      case  3:  authenticationCode  "0"+authenticationCode;  break;
      default:  authenticationCode  authenticationCode.substring(0,4);  break;
}
graphics.setColor(getRandColor(radomNumberColorBegin,radomNumberColorFinal));
graphics.drawString(authenticationCode,stringWidth,stringHight);
//System.out.println("authenticationCode"+authenticationCode);
  }

 

RandomPicClient.jsp
---------------------------------
<%@ page contentType="image/jpeg"
 import="java.awt.image.*,
         javax.imageio.*,
         et.command.pic.RandomPic,
         java.io.*"%>
<%
 response.setHeader("Pragma", "No-cache");
 response.setHeader("Cache-Control", "no-cache");
 response.setDateHeader("Expires", 0);
 double rand Math.random() 10000;
 RandomPic rp new RandomPic(Double.toString(rand));
 rp.drawClient();
 session.setAttribute("rand", rp.authenticationCode);
 BufferedImage image rp.image;
 try 
 {
  ImageIO.write(image, "JPEG", response.getOutputStream());
 catch (IOException e) {
  e.printStackTrace();
 }
%> 

 

index.jsp
---------------------------------
<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
 <img border='0' src='RandomPicClient.jsp'/>
</html>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值