servlet彩色验证码

原创 2005年03月02日 11:21:00

 servlet代码,代码内容写入session sRand中:

 servlet代码,代码内容写入session sRand中:

package CMCC;

import java.io.*;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.GraphicsEnvironment;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.IIOImage;

import javax.imageio.ImageIO;

import javax.imageio.ImageWriter;

import javax.imageio.plugins.jpeg.JPEGImageWriteParam;

import javax.imageio.stream.ImageOutputStream;

import javax.servlet.*;

import javax.servlet.http.*;

/**

 *

 * @author vLua

 * TODO

 * 2004-12-22 16:02:52

 */

public class VerifyCodeServlet extends HttpServlet

{

     private static final int IMAGE_WIDTH = 48;

     private static final int IMAGE_HEIGHT = 18;

     private static final int FONT_HEIGHT = 16;

     private static final String SVG_SOURCE1 = "<!DOCTYPE svg PUBLIC /"-//W3C//DTD SVG 1.0//EN/" /"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd/">"

         + "<svg width=/""

         + IMAGE_WIDTH

         + "/" height=/""

         + IMAGE_HEIGHT

         + "/" xmlns=/"http://www.w3.org/2000/svg/">"

         + "<text x=/"0/" y=/""

         + IMAGE_HEIGHT

         + "/" font-family=/"Arial/" font-size=/""

         + IMAGE_HEIGHT + "/" fill=/"black/">";

     private static final String SVG_SOURCE2 = "</text></svg>";

     /*

      * If graphic environment not avaliable(eg: run under linux console), use

      * svg instead of jpeg.

      */

     private boolean svgMode = false;

     private PrintWriter out = null;

     /*

      * (non-Javadoc)

      *

      * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,

      *      javax.servlet.http.HttpServletResponse)

      */

     protected void service(HttpServletRequest arg0, HttpServletResponse arg1)

         throws ServletException, IOException

     {

         if (sn == null)

              return;

         String vcode = sn2vcode();

         arg0.getSession().setAttribute("sRand",vcode) ;//代码内容写入session

         if (svgMode)

              outSVG(vcode, arg1);

         else

              outJPEG(vcode, arg1);

     }

     private void outSVG(String vcode, HttpServletResponse resp)

         throws IOException

     {

         resp.setContentType("image/svg+xml");

         resp.getOutputStream().print(SVG_SOURCE1 + vcode + SVG_SOURCE2);

     }

     private void outJPEG(String vcode, HttpServletResponse resp)

         throws IOException

     {

         resp.setContentType("image/jpeg");

         BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,

              BufferedImage.TYPE_INT_RGB);

         Random random = new Random();

         Graphics g = image.getGraphics();

         g.setColor(getRandColor(200, 250));

         g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);

         g.setFont(new Font("Times New Roman", Font.HANGING_BASELINE, 18));

         g.setColor(getRandColor(160, 200));

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

         {

              int x = random.nextInt(IMAGE_WIDTH);

              int y = random.nextInt(IMAGE_HEIGHT);

              int xl = random.nextInt(12);

              int yl = random.nextInt(12);

              g.drawLine(x, y, x + xl, y + yl);

         }

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

         {

              String rand = vcode.substring(i - 1, i);

              g.setColor(new Color(20 + random.nextInt(110), 20 + random

                   .nextInt(110), 20 + random.nextInt(110)));

              g.drawString(rand, 13 * (i - 1) + 0, 16);

         }

         ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(

              "jpeg").next();

         JPEGImageWriteParam params = new JPEGImageWriteParam(null);

         ImageOutputStream ios = ImageIO.createImageOutputStream(resp

              .getOutputStream());

         writer.setOutput(ios);

         writer.write(null, new IIOImage(image, null, null), params);

         writer.dispose();

         ios.close();

     }

     static String sn2vcode()

     {

         String sRand = "";

         Random random = new Random();

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

         {

              String rand=String.valueOf(random.nextInt(10));

              sRand+=rand;

         }

         return sRand;

     }

     /*

      * (non-Javadoc)

      *

      * @see javax.servlet.GenericServlet#init()

      */

     public void init() throws ServletException

     {

         try

         {

              GraphicsEnvironment.getLocalGraphicsEnvironment();

         }

         catch (Throwable e)

         {

              //run under linux console???

              svgMode = true;

         }

     }

     /**

      * 获得随机颜色

      * @param fc

      * @param bc

      * @return

      */

     public 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);

     }

}

在web.xml 中加入


 <servlet>
  <servlet-name>VerifyCodeServlet</servlet-name>
  <servlet-class>CMCC.VerifyCodeServlet</servlet-class>
  </servlet>
<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>/VerifyCodeServlet/*</url-pattern>
</servlet-mapping>

调用该servlet用http://hostname/VerifyCodeServlet/即可!

调用<img src=http://hostname/VerifyCodeServlet>

彩色扭曲英文验证码

  • 2013年03月18日 11:17
  • 4KB
  • 下载

扭曲彩色验证码(中英文双版)

  • 2014年11月08日 20:08
  • 12KB
  • 下载

英文,数字和中文混合的彩色验证码实现

功能描述:英文,数字和中文混合的彩色验证码是一种比较安全的验证码,虽然这样的验证码会给用户输入带来不便,但对于保障用户账号的安全还是值得的。本实例介绍实现英文,数字和中文混合验证码的彩色验证码的方法,...

登陆彩色验证码插件 for dvbbs6.1

  • 2005年12月27日 09:38
  • 32KB
  • 下载

用开源组件jcaptcha做jsp彩色验证码 ( 转http://blog.csdn.net/alexjjf/archive/2006/10/31/1359333.aspx)

安装Add jcaptcha-all.jar (provided in bin-distribution) and ehcache.jar (not provided see ehcache site...
  • fjfdszj
  • fjfdszj
  • 2011年02月16日 16:27
  • 730

jsp彩色验证码 txt

  • 2008年10月07日 13:40
  • 5KB
  • 下载

基于jsp的4位随机彩色验证码

  • 2008年12月16日 10:44
  • 1KB
  • 下载

JSP彩色验证码,生成有4个随机数字和杂乱背景的图片,数字和背景颜色会改变,服务器端刷新

生成有4个随机数字和杂乱背景的图片,数字和背景颜色会改变,服务器端刷新(用history.go(-1)也会变)原型参考ALIBABA http://china.alibaba.com/member/s...

jsp彩色验证码简单完美版

  • 2010年11月10日 19:58
  • 2KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:servlet彩色验证码
举报原因:
原因补充:

(最多只允许输入30个字)