JSP验证码大全之Servlet实现(一)

在以上的内容中阐述了在JSP中产生并实现了 数字验证码 中文验证码 的过程,以及如何在JSP中 验证码调用和解决中文问题 ,并对验证码的使用做了分析。本文将介绍另一种J2EE中验证码的产生跟使用,即在Servlet中定义验证码的产生并使用,通过将验证码的生成封装到JAVA类中,更好的达到代码跟页面分离的效果,因此提倡使用该方法。
   五、Servlet中实现四位数字验证码
   以下为在Servlet中实现四位数字验证码的源码分析。
 

import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;

   public class AuthCodeServlet extends HttpServlet {
   // 处理post
    public void doPost(HttpServletRequest req,HttpServletResponse res)
    throws ServletException,IOException {
    doGet(req,res);
 }

   //设置字体
   private Font mFont=new Font("Times New Roman", Font.PLAIN,16);

   public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {

     HttpSession session=request.getSession();
     response.setContentType("image/gif");
     response.setHeader("Pragma","No-cache");
     response.setHeader("Cache-Control","no-cache");
     response.setDateHeader("Expires", 0);
     int width=70; //验证码默认长度
     int height=24; //验证码默认宽度
     if(request.getParameter("w")!=null && !request.getParameter("w").equals(""))
      width = Integer.parseInt(request.getParameter("w"));
     if(request.getParameter("h")!=null && !request.getParameter("h").equals(""))
      height = Integer.parseInt(request.getParameter("h"));
                
     ServletOutputStream out=response.getOutputStream(); //获取输出流
     BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //新建验证图片,并设置验证码图片的大小
     Graphics gra=image.getGraphics(); //获取图形上下文
     Random random=new Random();
     gra.setColor(getRandColor(260,210));    //设置验证码的图片背景色
     gra.fillRect(0,0,width,height);
     gra.setColor(Color.BLUE); //设置字体色为蓝色
     gra.setFont(mFont); //设置定义的字体格式

     // 随机产生254条干扰直线,使图象中的验证码不易被解析程序分析到
     gra.setColor(getRandColor(110,240));
     for (int i=0;i<254;i++)
     {
      int x = random.nextInt(width);
      int y = random.nextInt(height);
             int xl = random.nextInt(63);
             int yl = random.nextInt(64);
      gra.drawLine(x,y,x+xl,y+yl);
     }

     // 取随机产生的验证码(4位数字)
     String sRand="";
     for (int i=0;i<4;i++){
     String rand=String.valueOf(random.nextInt(353));
     sRand+=rand;
     // 将认证码显示到图象中
      gra.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
     //调用随机函数构建随机颜色三素
         gra.drawString(rand,13*i+6,16);
     }
         session.setAttribute("authCode",sRand);
         JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
         encoder.encode(image);

 }

   static Color getRandColor(int ff,int cc){
          //给定范围获得随机颜色
          Random random = new Random();
          if(fc>255) ff=255;
          if(bc>255) cc=255;
          int r=ff+random.nextInt(cc-ff);
          int g=ff+random.nextInt(cc-ff);
          int b=ff+random.nextInt(cc-ff);
          return new Color(r,g,b);
   }
  
   static public String getAuthCode(HttpSession session){
    //返回验证码
    return (String)session.getAttribute("AuthCode");
   }
}

   以上即是通过Servlet中创建并生产数字验证码的源码分析,下一篇文章将介绍在Servlet中生成数字字母混合验证码的过程,并说明如何对Servlet中的验证码进行使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值