欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《生成登录验证码V1.0》

程序员文章站 2024-03-20 13:46:58
...

 1.登录页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
	<!-- <script type="text/javascript">
		function changeImg(){
			document.getElementById("img").src="CheckCodeServlet?a="+Math.random();//new Date()
		}
		
	</script>
<body>
	<form action="LoginServlet" method="post">
		用户名:<input type="text" name = "usrename" /><br>
		密码:<input type="password" name = "password" /><br>
		验证码:<input type="text" name = "checkcode" size="10"/><img src="CheckCodeServlet"><a id="img" href="javascript:changeImg()">看不清,换一张?</a><br>
		<input type="submit" value="登录">
	</form> -->
	<script >
	function changeImg(){
		//因为路径不变,会有缓存,所以?加上一个每次回变化的值:new Date();或者Math.random();带值并不影响访问路径,只是地址栏会发生变化,防止缓存
		document.getElementById('checkcode').src="CheckCodeServlet?aaa="+Math.random();
	}
</script>

</head>
<body>
	<form action="LoginServlet" method = "get">
	<font size="6">会员登录</font>
	<font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;USER LOGIN</font><br/>
	用户名:<input type="text" name = "username"><br/>
	密码:<input type="password" name = "userpwd"><br/>
	验证码:<input type="text" name = "checkcode" size="10"><img id="checkcode" src="CheckCodeServlet"><a href="javascript:changeImg()">看不清?换一张</a><br/>
	<input type="submit" value="登录">
	</form>
</body>
</html>

2.生成验证码的Servlet

package com.offcn.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
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;

public class CheckCodeServlet extends HttpServlet {

	//图片是通过src的地址链接进行访问,所以是get请求
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.生成验证码图片的宽度和高度
		int width=60;
		int height=30;
		
		//2.设置一个字符串,从字符串中随机抽取4个字符,放在验证码的图片上
		String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
		Random random = new Random();
		
		//3创建一个图片
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				
		//4获得画板
		Graphics g = image.getGraphics();
		
		//5 填充一个矩形
		
		// * 设置颜色
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);
		
		g.setColor(Color.WHITE);
		g.fillRect(1, 1, width-2, height-2);
		// * 设置字体:宋体,加粗|用的什么字体,大小
		g.setFont(new Font("宋体", Font.BOLD|Font.ITALIC, 25));
				
				
		//6 写随机字
		for(int i = 0 ; i < 4 ; i ++){
			// 设置颜色--随机数
			g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
					
			// 获得随机字
			int index = random.nextInt(data.length());
			String str = data.substring(index, index + 1);
					
			// 写字符到图片上,左上位置
			g.drawString(str, width/6 * (i + 1), 20);
					
		}
				
		//7 干扰线
		for(int i = 0 ; i < 3 ; i ++){
			// 设置颜色--随机数
			g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
			// 随机绘制线:两点确定一条线,两个随机数确定一个坐标点
			g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width),random.nextInt(height));
			// 随机点:两个随机数确定一个圆心,半径,粗细
			g.drawOval(random.nextInt(width), random.nextInt(height), 2, 2);
		}
				
		//8end 将图片响应给浏览器
		ImageIO.write(image, "jpg", response.getOutputStream());
	}
}