servlet图片验证码

3/8/2017来源:ASP.NET技巧人气:4416

创建web工程,新建VerifyCode.java
package com.xmyself.model;
 
import java.awt.image.BufferedImage;
 
public class VerifyCode {
    PRivate String code;
    private BufferedImage image;
 
    public VerifyCode(String code, BufferedImage image) {
        this.code = code;
        this.image = image;
    }
 
    public String getCode() {
        return code;
    }
 
    public void setCode(String code) {
        this.code = code;
    }
 
    public BufferedImage getImage() {
        return image;
    }
 
    public void setImage(BufferedImage image) {
        this.image = image;
    }
}

生成随机验证码和图片的类VerifyCodeFactory.java,这并不是静态工厂,只是一个名称而已

package com.xmyself.factory;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
 
import com.xmyself.model.VerifyCode;
 
public class VerifyCodeFactory {
    public static VerifyCode create() {
        String code = getRandomString(4);
        return new VerifyCode(code, drawImage(code));
    }
     
    private static String getRandomString(int length) {
        if (length < 0 || length > 8) length = 4;
        String scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer code = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            code.append(scope.charAt(random.nextInt(scope.length() - 1)));
        }
        return code.toString();
    }
     
    private static Color getRandomColor() {
        Random random = new Random();
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);
        return new Color(r, g, b);
    }
     
    private static BufferedImage drawImage(String code) {
        int width = 80;
        int height = 34;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //画边框和背景
        Graphics brush = image.getGraphics();
        brush.setColor(getRandomColor());
        brush.fillRect(0, 0, width - 1, height - 1);
        brush.drawRect(0, 0, width - 1, height - 1);
        //画验证码
        brush.setColor(getRandomColor());
        brush.setFont(new Font("Times New Roman", Font.ITALIC, 28));
        brush.drawString(code, 7, 22);
 
        brush.dispose();
        return image;
    }
}
创建servlet类VerifyCodeServlet.java
package com.xmyself.servlet;
 
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.xmyself.factory.VerifyCodeFactory;
import com.xmyself.model.VerifyCode;
 
public class VerifyCodeServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
 
   public void init(ServletConfig config) throws ServletException {
   }
 
   public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws IOException, ServletException {
   }
 
   public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws IOException, ServletException {
       VerifyCode vc = VerifyCodeFactory.create();
       request.getsession().setAttribute("verifyCode", vc.getCode());
       ImageIO.write(vc.getImage(), "JPEG", response.getOutputStream());
   }
 
   public void destroy() {
   }
}
配置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"
    version="2.5">
    <display-name>servlet</display-name>
      
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>com.xmyself.servlet.VerifyCodeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>
启动web工程,访问以下链接即可看到验证码图片
http://localhost:8080/servlet/test