servlet制作驗證碼和驗證碼的點擊切換



做了一個小例子,web在登陸的時候一般都有驗證碼驗證,這里用servlet來制作一個驗證碼。


servlet:

 package com.zhkunion.util;
 
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.util.Random;
 import javax.imageio.ImageIO;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 /**
  * 生成圖片驗證碼的servlet
  * @author maojycom
  * 創建日期: 2014-11-6 上午11:44:02
  * @version 1.0.0
  */
 public class CheckCode extends HttpServlet{
	
	private static final long serialVersionUID = 2011594857610014291L;
	private static final int WIDTH = 80;
	private static final int HEIGHT = 26;
	private final String BASES = "123456789ABCDEFGEFGHJKLMNPQapcdefghlkjmnpgwq";
 
   public void doGet(HttpServletRequest request, HttpServletResponse response) 
		   throws ServletException, IOException{
		    
	   	    //聲明返回的MIME類型
	   		response.setContentType("image/jpeg");
		 
		     BufferedImage image = new BufferedImage(WIDTH, HEIGHT, 1);
		 
		     Graphics g = image.getGraphics();
		 
		     setBackground(g);
		 
		     setBorder(g);
		 
		     String num = drawRandomNum((Graphics2D)g);
		     
		     //把驗證碼存放到session中去
		     request.getSession().setAttribute("checkCode", num);
		 
		     drawRandomLine(g);
		     
		     //以圖像流方式輸出圖片
		     ImageIO.write(image, "jpg", response.getOutputStream());
   }
 
   /**
    * 隨機生成數字
    * @param g
    * @return
    */
   public String drawRandomNum(Graphics2D g) {
	   
     g.setColor(Color.red);
     g.setFont(new Font("宋體", 1, 20));
 
     StringBuffer sb = new StringBuffer();
 
     int x = 5;
    for (int i = 0; i < 4; i++){
       String ch = BASES.charAt(new Random().nextInt(BASES.length())) + "";
       sb.append(ch);
       g.drawString(ch, x, 20);
       x += 20;
     }
       return sb.toString();
   }
   /**
    * 隨機畫干擾線
    * @param g
    */
   private void drawRandomLine(Graphics g) {
	   g.setColor(Color.GRAY);
 
	   for (int i = 0; i < 10; i++) {
		   int x1 = new Random().nextInt(80);
		   int y1 = new Random().nextInt(26);
 
		   int x2 = new Random().nextInt(80);
		   int y2 = new Random().nextInt(26);
 
		   g.drawLine(x1, y1, x2, y2);
     }
   }
   /**
    * 設置邊框
    * @param g
    */
   public void setBorder(Graphics g){
	   g.setColor(Color.BLUE);
	   g.drawRect(1, 1, 78, 24);
   }
 
   /**
    * 設置背景
    * @param g
    */
   public void setBackground(Graphics g) {
     g.setColor(Color.WHITE);
     g.fillRect(0, 0, 80, 80);
   }
 
   public void doPost(HttpServletRequest request, HttpServletResponse response) 
		   throws ServletException, IOException{
     doGet(request, response);
   }
 }

在web.xml中配置該servlet驗證碼:

<!-- 配置驗證碼的路徑 -->
    <servlet>
    	<servlet-name>checkCode</servlet-name>
    	<servlet-class>net.seehope.util.CheckCode</servlet-class>
    </servlet>
    <servlet-mapping>
    	<servlet-name>checkCode</servlet-name>
    	<url-pattern>/checkCode.jpg</url-pattern>
    </servlet-mapping>

在頁面中使用:

        <script type="text/javascript">
		//實現當點擊圖片是能切換
		function change(img){
			img.src = img.src + "?" + new Date().getTime();
	}
	</script>  
  </head>  
  <body>
  		驗證碼:<img alt="驗證碼" src="checkCode.jpg" onclick="change(this)">
  </body>

補充:有時從服務器端生成的驗證碼跟從JSP頁面填寫的驗證碼不一致,因為服務器端生成的驗證碼是存放到session中的,

而加載JSP比加載session要快,這導致了JSP中顯示的驗證碼是剛生成的,但session中的驗證碼還是上一次的,還沒有清除、更新。

1,及時清除session中的驗證碼,確保能讀到最新的驗證碼;

2,有JSP生成驗證碼,再傳給后台,讓后台在驗證碼上畫干擾線及顏色背景等。







注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com