2011年10月24日 星期一

驗證圖形驗證碼

index.jsp ----->主頁面

<%@ page language="java" contentType="text/html; charset=BIG5"%>

<form method="post" action="Verification.jsp">
    <img src="dynamicImage"/><br/>
    <input type="text" name="value"></input>
    <input type="submit"></input>
</form>


Verification.jsp ----->驗證

<%@ page language="java" contentType="text/html; charset=big5"%>

<%
    if( request.getParameter("value").equals(session.getAttribute("number")) ){
        out.print("驗證碼正確");
    }else{
        out.print("驗證碼錯誤");
    }
%>


DynamicImage.java ---->產生動態圖形驗證碼

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DynamicImage extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.setContentType("image/jpeg");

        BufferedImage bi = new BufferedImage(50,30,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, 50, 30);
        g2d.setColor(Color.black);
        g2d.drawRect(5, 5, 40, 20);

        StringBuilder sbl = new StringBuilder();
        while(sbl.length() < 4){
            sbl.append(Integer.toString((int)(Math.random() * 9)));
        }

        session.setAttribute("number", sbl.toString());
  
        g2d.setFont(new Font("Default",Font.BOLD,13));
        g2d.drawString(sbl.toString(), 10, 20);
  
        for(int i = 0; i < 20; i++){
            int x = (int)(Math.random() * 40) + 5;
            int y = (int)(Math.random() * 20) + 5;
   
            g2d.fillOval(x, y, 1, 1);
        }
  
        ServletOutputStream sos = response.getOutputStream();
  
        Iterator ite = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter iw = (ImageWriter)ite.next();
  
        ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
        iw.setOutput(ios);
        iw.write(bi);
        ios.flush();
        sos.close();
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>DynamicImage</servlet-name>
    <servlet-class>com.morty.dynamicimage.DynamicImage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DynamicImage</servlet-name>
    <url-pattern>/dynamicImage</url-pattern>
  </servlet-mapping>

</web-app>

沒有留言:

張貼留言