`

验证码的生成

 
阅读更多
生成图形验证码:
package com.servlet;

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

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ValidationCode extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String codeChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	private  Color getRandomColor(int minColor , int maxColor){
		Random random = new Random();
		if(minColor > 255){
			minColor = 255;
		}
		if(maxColor > 255){
			maxColor = 255;
		}
		if(maxColor == minColor){
			maxColor = 10;
			minColor = 5;
		}
		int red = minColor + random.nextInt(Math.abs(maxColor-minColor));
		int green = minColor + random.nextInt(Math.abs(maxColor-minColor));
		int blue = minColor + random.nextInt(Math.abs(maxColor-minColor));
		return new Color(red,green,blue);
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		int charsLength = codeChars.length();
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setDateHeader("expires", 0);
		int width = 90;
		int height = 20;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		Random random = new Random();
		g.setColor(this.getRandomColor(180, 250));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Times New Roman",Font.ITALIC,height));
		g.setColor(this.getRandomColor(120, 180));
		StringBuilder validationCode = new StringBuilder();
		String [] fontNames = {"Times New Roman","Book antqua","Arial"};
		for(int i = 0; i < 3+random.nextInt(3);i++){
			g.setFont(new Font(fontNames[random.nextInt(3)],Font.ITALIC,height));
			char codeChar = codeChars.charAt(random.nextInt(charsLength));
			validationCode.append(codeChar);
			g.setColor(this.getRandomColor(10, 100));
			g.drawString(String.valueOf(codeChar), 16*i+random.nextInt(7), height-random.nextInt(6));
		}
		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(5*60);
		session.setAttribute("validation_code", validationCode.toString());
		System.out.println(validationCode.toString());
		g.dispose();
		OutputStream os = response.getOutputStream();
		ImageIO.write(image, "JPEG", os);
	}
}



web.xml文件配置
	<servlet>
		<servlet-name>validationCode</servlet-name>
		<servlet-class>com.servlet.ValidationCode</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>validationCode</servlet-name>
		<url-pattern>/validationCode</url-pattern>
	</servlet-mapping>

jsp测试页面:
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
	<head>
		<link type="text/css" rel="stylesheet" href="css/style.css" />
		<title>用户登录</title>
		<script type="text/javascript">
function refresh() {
	var img = document.getElementById("img_validation_code")
	img.src = "validationCode?" + Math.random();
}
</script>
	</head>
	<body>
		<ul>
			<li>
				<div class="name_list font_space float_left">
					验证码:
				</div>
				<div class="input_list1 font_space float_left">
					<input type="text" id="validation_code" name="validation_code"
						style="width: 60px; margin-top: 2px" size="30" maxlength="30" />
					<img id="img_validation_code" src="validationCode" />
					<input type="button" value="刷新" onclick="refresh()" />
					&nbsp;&nbsp;
					<font color="#FF0000">${requestScope.codeError}</font>
				</div>
			</li>
		</ul>
	</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics