国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

servlet圖片驗證碼

2019-11-06 06:02:21
字體:
來源:轉載
供稿:網友
創建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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海安县| 盐源县| 德清县| 中宁县| 金山区| 昭平县| 瓦房店市| 浮山县| 登封市| 奇台县| 吴桥县| 宁城县| 腾冲县| 三都| 高碑店市| 通渭县| 瑞安市| 抚松县| 竹北市| 横峰县| 木里| 桐庐县| 宕昌县| 青铜峡市| 泊头市| 沈阳市| 孟津县| 阜新市| 繁昌县| 两当县| 宾阳县| 义马市| 壶关县| 吐鲁番市| 宣化县| 嫩江县| 南充市| 普格县| 威海市| 社旗县| 安义县|