验证码原始生成方法

[java]  view plain copy
  1. package com.tan.guo;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Graphics;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.IOException;  
  7. import java.io.PrintWriter;  
  8. import java.util.Random;  
  9.   
  10. import javax.imageio.ImageIO;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.   
  16. /** 
  17.  * 功能: 
  18.  * @author TanZi 
  19.  * @time 2015-4-11上午11:16:21 
  20.  */  
  21. public class ImageServlet extends HttpServlet {  
  22.   
  23.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  24.             throws ServletException, IOException {  
  25.           
  26.         BufferedImage bi=new BufferedImage(6822, BufferedImage.TYPE_INT_RGB);//创建一个图片  
  27.         Graphics g=bi.getGraphics();              //创建画笔  
  28.         Color c=new Color(200,150,225);  
  29.         g.setColor(c);  
  30.         g.fillRect(006822);                //设置画的位置  
  31.           
  32.         char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();    //设置画的内容  
  33.         Random r=new Random();  
  34.         int len=ch.length,index;  
  35.         StringBuffer sb=new StringBuffer();                    //用来保存生成的验证码字符  
  36.         for(int i=0;i<4;i++){  
  37.             index=r.nextInt(len);  
  38.             g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));  
  39.             g.drawString(ch[index]+"", (i*15)+318);  
  40.             sb.append(ch[index]);  
  41.         }  
  42.         request.getSession().setAttribute("piccode", sb.toString());   //把生成的验证码字符放到session  
  43.         ImageIO.write(bi, "JPG", response.getOutputStream());        //写回图片  
  44.     }  
  45.   
  46.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  47.             throws ServletException, IOException {  
  48.   
  49.         this.doGet(request, response);  
  50.     }  
  51.   
  52.       
  53. }  

jsp上的编写:

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.     <script type="text/javascript">  
  22.       
  23.     function reloadCode(){  
  24.         var time=new Date().getTime();  
  25.         document.getElementById("imagecode").src="<%=request.getContextPath()%>/servlet/ImageServlet?d="+time;  
  26.     }  
  27.       
  28.     </script>  
  29.   </head>  
  30.     
  31.   <body>  
  32.     验证码:<input type="text" name="checkcode"/>  
  33.     <img alt="验证码" id="imagecode" src="<%=request.getContextPath()%>/servlet/ImageServlet"/>  
  34.     <a href="javascript:reloadCode();">看不清</a>  
  35.     <br>  
  36.   </body>  
  37. </html>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值