验证码实现 JSP

jsp前端语句

<img src="/first/AuthCodeServlet" 
onclick="this.src='/first/AuthCodeServlet?'+Math.random()">

web.xml

<servlet>
<servlet-name>AuthCodeServlet</servlet-name>
<servlet-class>first.AuthCodeServlet</servlet-class>
<init-param>
<param-name>count</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>width</param-name>
<param-value>75</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>25</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>AuthCodeServlet</servlet-name>
<url-pattern>/AuthCodeServlet</url-pattern>
</servlet-mapping>``


# servlet

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

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;

public class AuthCodeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private static Random r   = new Random();
private static char[] chs = "1234567890".toCharArray();
private static int    NUMBER_OF_CHS;
private static int    IMG_WIDTH;
private static int    IMG_HEIGHT;

public void init(ServletConfig config) throws ServletException {
    String count = config.getInitParameter("count");
    NUMBER_OF_CHS = Integer.parseInt(count);
    String width = config.getInitParameter("width");
    IMG_WIDTH = Integer.parseInt(width);
    String height = config.getInitParameter("height");
    IMG_HEIGHT = Integer.parseInt(height);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("获得验证码图片");
    BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB); // 实例化BufferedImage
    // Graphics2D g = (Graphics2D) image.getGraphics();
    Graphics2D g = image.createGraphics();
    g.setColor(new Color(200, 200, 255));// 验证码图片的背景颜色
    g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT); // 图片的边框
    g.setFont(new Font("隶书", Font.BOLD, 15));// 设定字体

    StringBuffer sb = new StringBuffer(); // 用于保存验证码字符串
    for (int i = 0; i < NUMBER_OF_CHS; i++) {
        int index = r.nextInt(chs.length); // 随机一个下标
        g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); // 随机一个颜色
        g.drawString(chs[index] + "", 15 * i + 3, 18); // 画出字符
        sb.append(chs[index]); // 验证码字符串
    }

    request.getSession().setAttribute("authCode", sb.toString()); // 将验证码字符串保存到session中
    ImageIO.write(image, "jpg", response.getOutputStream()); // 向页面输出图像
    // response.getWriter().write("aaaa");
    // request.getSession().getAttribute("authCode");
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值