生成验证码的Servlet,生成验证码的Se,成验证码的Ser,验证码的Serv,证码的Servl,码的Servle 生成验证码的Servlet----编程资料集中营--八角123--bajiao123.com
编程资料集中营
 | 网站首页 | 文章中心 | 编程资料2 | 软件下载 | BT下载 | 八卦星闻 | 音乐在线 | 在线游戏 | 免费电影 | 进入问吧 | 
生成验证码的Servlet, 生成验证码的Servlet,如图: //Fig.5.6_02:ImageCodeMakerServlet.java//读取图像文件并生成验证码的Servletpackagecom.fatcat.webchart;importjavax.servlet.*;importjavax.s,
您现在的位置: 编程资料,学习资料,c,c++,vc,vc++,java,jsp,j2ee,j2me,asp,php >> 文章中心 >> JAVA 专区 >> JSP 技术 >> 文章正文
【字体:
生成验证码的Servlet   进入问吧

本站地址:http://www.bajiao123.com

作者:admin    文章来源:本站    点击数:    更新时间:2007-5-25    

生成验证码的Servlet

 

   
 生成验证码的Servlet,如图:



// Fig. 5.6_02:  ImageCodeMakerServlet.java

// 读取图像文件并生成验证码的Servlet

package com.fatcat.webchart;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import java.awt.image.*;

import java.awt.*;

import javax.imageio.*;

public class ImageCodeMakerServlet extends HttpServlet

{

    String codeNumbers = "";

    int width = 240, height = 60;

    // 处理 HTTP get 请求

    public void doGet(HttpServletRequest request, HttpServletResponse response)

                      throws ServletException, IOException

    {

        // 清空缓冲区

        response.reset();

        // 注意这里的MIME类型

        response.setContentType("image/png");

        // 设置页面不缓存

        response.setHeader("Pragma", "No-cache");

        response.setHeader("Cache-Control", "no-cache");

        response.setDateHeader("Expires", 0);

        // 创建一个 240X60 的图像

        BufferedImage image = new BufferedImage(width, height,

                                                BufferedImage.TYPE_INT_RGB);

        // 得到图形环境对象 g

        Graphics g = image.getGraphics();

        // 填充背景

        g.setColor(Color.WHITE);

        g.fillRect(0, 0, width, height);

        for (int i = 0; i < 4; i++)

        {

            drawCode(g, i, request);

        }

       // drawNoise(g, 30);

        // 绘制边框

        g.setColor(Color.black);

        g.drawRect(0, 0, width - 1, height - 1);

        // 将验证码内容保存进session中

        HttpSession session = request.getSession(true);

        session.setAttribute("codeNumbers", codeNumbers);

        

		// 重设字符串

        codeNumbers = "";

        // 利用ImageIO类的write方法对图像进行编码

        ServletOutputStream sos = response.getOutputStream();

        ImageIO.write(image, "PNG", sos);

        sos.close();

    }

    // 加载图像文件并绘制验证码

    public void drawCode(Graphics graphics, int i, HttpServletRequest request)

    {

        int number = (int)(Math.random() * 10);

         String imageFilePath = 
          request.getRealPath("\\images\\" + number + ".gif"; File imageFile = new File(imageFilePath); Image gifFile = null; try { gifFile = ImageIO.read(imageFile); } catch (Exception e) { System.out.println(e); } graphics.drawImage(gifFile, i* 60, 0, null); codeNumbers += number; } // 绘制干扰线 public void drawNoise(Graphics graphics, int lineNumber) { graphics.setColor(Color.YELLOW); for (int i = 0; i < lineNumber; i++) { int pointX1 = 1 + (int)(Math.random() * width); int pointY1 = 1 + (int)(Math.random() * height); int pointX2 = 1 + (int)(Math.random() * width); int pointY2 = 1 + (int)(Math.random() * height); graphics.drawLine(pointX1, pointY1, pointX2, pointY2); } } // 处理 HTTP post 请求, 和doGet一样 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } /************************************************************************** * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). * * All Rights Reserved. * * * * DISCLAIMER: The authors of this code have used their * * best efforts in preparing the code. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these codes. The authors * * shall not be liable in any event for incidental or consequential * * damages in connection with, or arising out of, the furnishing, * * performance, or use of these programs. * **************************************************************************/


其它文件请下载。


 


   

进入问吧

本站地址:http://www.bajiao123.com

文章录入:admin    责任编辑:admin 
编程资料集中营