验证码的生成,并且“看不清,换一张”

Java代码   收藏代码
  1. public class AuthImg extends HttpServlet {  
  2.     private static final String CONTENT_TYPE = "text/html; charset=UTF-8";  
  3.   
  4.     private Font mFont;  
  5.   
  6.     public AuthImg() {  
  7.         mFont = new Font("Times New Roman"018);  
  8.     }  
  9.   
  10.     public void init() throws ServletException {  
  11.     }  
  12.   
  13.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.         String authCode = genAuthCode(4);  
  16.         AuthCodeCookie authCodeCookie = new AuthCodeCookie(request, response);  
  17.         authCodeCookie.addAuthCode(authCode);//保存验证码到cookies  
  18.         response.setContentType("image/jpeg");  
  19.         javax.servlet.ServletOutputStream out = response.getOutputStream();  
  20.         int width = 60;  
  21.         int height = 20;  
  22.         BufferedImage image = new BufferedImage(width, height, 1);  
  23.         Graphics gra = image.getGraphics();  
  24.         Random random = new Random();  
  25.         gra.setColor(getRandColor(200250));  
  26.         gra.fillRect(11, width, height);  
  27.         gra.setColor(getRandColor(160200));  
  28.         for (int i = 0; i < 155; i++) {  
  29.             int x = random.nextInt(width);  
  30.             int y = random.nextInt(height);  
  31.             int xl = random.nextInt(12);  
  32.             int yl = random.nextInt(12);  
  33.             gra.drawLine(x, y, x + xl, y + yl);  
  34.         }  
  35.   
  36.         gra.setFont(mFont);  
  37.         for (int i = 0; i < 4; i++) {  
  38.             char c = authCode.charAt(i);  
  39.             gra.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));  
  40.             gra.drawString(String.valueOf(c), 13 * i + 616);  
  41.         }  
  42.   
  43.         gra.dispose();  
  44.         ImageIO.write(image, "JPEG", out);  
  45.     }  
  46.   
  47.     Color getRandColor(int fc, int bc) {  
  48.         Random random = new Random();  
  49.         if (fc > 255)  
  50.             fc = 255;  
  51.         if (bc > 255)  
  52.             bc = 255;  
  53.         int r = fc + random.nextInt(bc - fc);  
  54.         int g = fc + random.nextInt(bc - fc);  
  55.         int b = fc + random.nextInt(bc - fc);  
  56.         return new Color(r, g, b);  
  57.     }  
  58.   
  59.     public String genAuthCode(int length) {  
  60.         if (length < 1)  
  61.             return null;  
  62.         String strChars[] = { "1""2""3""4""5""6""7""8""9" };  
  63.         StringBuffer strPassword = new StringBuffer();  
  64.         int nRand = (int) Math.round(Math.random() * 100D);  
  65.         for (int i = 0; i < length; i++) {  
  66.             nRand = (int) Math.round(Math.random() * 100D);  
  67.             strPassword.append(strChars[nRand % (strChars.length - 1)]);  
  68.         }  
  69.   
  70.         return strPassword.toString();  
  71.     }  
  72.   
  73.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  74.             throws ServletException, IOException {  
  75.         doGet(request, response);  
  76.     }  
  77.   
  78.     public void destroy() {  
  79.     }  
  80.   
  81. }  


html: 
function refresh() { 
var img = document.getElementById("checkCodeImg"); 
now = new Date(); 
img.src = "authimg?code="+now.getTime(); 


<IMG height=18 src='authimg' id="checkCodeImg" > 
<a href="#" οnclick="javascript:refresh();">看不清,换一张</a> 

web.xml: 
<servlet> 
    <servlet-name>authimg</servlet-name> 
    <servlet-class>com.worthtech.app.servlet.AuthImg</servlet-class> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>authimg</servlet-name> 
    <url-pattern>/authimg</url-pattern> 
  </servlet-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值