资源描述:
《java代码生成验证码(servlet)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、java验证码代码(转)产生图片servletimportjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.image.BufferedImage;importjava.util.Random;importjavax.imageio.ImageIO;importjavax.servlet.ServletException;importjavax.servlet.ServletOutputStream;importjavax.servl
2、et.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/***Packagename:com.skcc.sample.actionFilename:VerifyCodeServlet.java*Author:AdministratorDate:2008-9-4Description:VerifyCod
3、eServlet*/publicclassVerifyCodeServletextendsHttpServlet{/****/privatestaticfinallongserialVersionUID=1L;/***验证码图片的宽度。*/privateintwidth=60;/***验证码图片的高度。*/privateintheight=20;/***验证码字符个数第9页共9页*/privateintcodeCount=4;/***xx*/privateintxx=0;/***字体高度*/privateintfontHeight;/
4、***codeY*/privateintcodeY;/***codeSequence*/char[]codeSequence={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};/***初始化验证图片属性*/publicvoidinit()throwsServletException{//从web.
5、xml中获取初始信息//宽度StringstrWidth=this.getInitParameter("width");//高度StringstrHeight=this.getInitParameter("height");//字符个数StringstrCodeCount=this.getInitParameter("codeCount");//将配置的信息转换成数值try{if(strWidth!=null&&strWidth.length()!=0){width=Integer.parseInt(strWidth);第9页共9页}
6、if(strHeight!=null&&strHeight.length()!=0){height=Integer.parseInt(strHeight);}if(strCodeCount!=null&&strCodeCount.length()!=0){codeCount=Integer.parseInt(strCodeCount);}}catch(NumberFormatExceptione){e.printStackTrace();}xx=width/(codeCount+1);fontHeight=height-2;codeY
7、=height-4;}/***@paramreq*@paramresp*@throwsServletException*@throwsjava.io.IOException*/protectedvoidservice(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,java.io.IOException{//定义图像bufferBufferedImagebuffImg=newBufferedImage(width,height,BufferedI
8、mage.TYPE_INT_RGB);Graphics2Dgd=buffImg.createGraphics();//创建一个随机数生成器类Randomrandom=newRandom();//将图像填充为白色gd.se