使用JSP生成验证码
    在开发中验证码是比较常用到有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试的方式。
此演示程序包括三个文件:
1.index.jsp:登录页面
2.image.jsp:生成验证码图片页面
3.result.jsp:结果页面 
【页面显示】
【页面代码】
1.index.jsp 
xml 代码
01.<html><body>      
02.<form method=post action="result.jsp">      
03.<input type=text name=input maxlength=4>      
04.<img border=0 src="image.jsp">      
05.<input type="submit" value="submit">      
06.</form></body></html>  
[注意]:(1)使用maxlength属性来限制输入字符;
(2)使用<img>标签来显示生成的验证码图片. 
2.image.jsp
01.<%@ page contentType="image/JPEG"  
02.    import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"  
03.    pageEncoding="GBK"%>  
04.<%!Color getRandColor(int fc, int bc) {//给定范围获得随机颜色   
05.        Random random = new Random();   
06.        if (fc > 255)   
07.            fc = 255;   
08.        if (bc > 255)   
09.            bc = 255;   
10.        int r = fc + random.nextInt(bc - fc);   
11.        int g = fc + random.nextInt(bc - fc);   
12.        int b = fc + random.nextInt(bc - fc);   
13.        return new Color(r, g, b);   
14.    }%>  
15.<%   
16.    //设置页面不缓存   
17.    response.setHeader("Pragma", "No-cache");   
18.    response.setHeader("Cache-Control", "no-cache");   
19.    response.setDateHeader("Expires", 0);   
20.  
21.    // 在内存中创建图象   
22.    int width = 60, height = 20;   
23.    BufferedImage image = new BufferedImage(width, height,   
24.            BufferedImage.TYPE_INT_RGB);   
25.  
26.    // 获取图形上下文   
27.    Graphics g = image.getGraphics();   
28.  
29.    //生成随机类   
30.    Random random = new Random();   
31.  
32.    // 设定背景色   
33.    g.setColor(getRandColor(200, 250));   
34.    g.fillRect(0, 0, width, height);   
35.  
36.    //设定字体   
37.    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));   
38.  
39.    //画边框   
40.    //g.setColor(new Color());   
41.    //g.drawRect(0,0,width-1,height-1);   
42.  
43.    // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
44.    g.setColor(getRandColor(160, 200));   
45.    for (int i = 0; i < 100; i++) {   
46.        int x = random.nextInt(width);   
47.        int y = random.nextInt(height);   
48.        int xl = random.nextInt(12)