当前位置 : 主页 > 编程语言 > c++ >

图片验证码(servlet)

来源:互联网 收集:自由互联 发布时间:2021-06-30
servlet格式 package com.mj.servlet;import java.awt.BasicStroke;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.
servlet格式
package com.mj.servlet;

import java.awt.BasicStroke;
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.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@WebServlet("/checkm.do")
public class CheckM extends HttpServlet{

	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		// 定义图片宽高

		int h = 35;
		int len = 4;
		int w = len*25;
		Random r = new Random();
		HttpServletRequest request = (HttpServletRequest)req;
		// 实例化缓冲图片

		BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
		// 获取画笔队像

		//Graphics g = image.getGraphics();

		Graphics2D graphics = (Graphics2D) image.getGraphics();
		// 设置背景颜色

		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, w, h);
		
		//设置文字

		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		StringBuilder checknum = new StringBuilder(len);
		for(int i=0;i
网友评论