废话不多说,直接上代码。先看看文件的位置
ValidateCodeServlet.java
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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ValidataCoed
*/
@WebServlet("/ValidataCoed")
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ValidateCodeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
public Color getRandomColor(int fc,int bc){
Random random = new Random();
Color randomColor = null;
if(fc>255) fc=255;
if(bc>255) bc=255;
//设置个0-255之间的随机颜色值
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
randomColor = new Color(r,g,b);
return randomColor;//返回具有指定红色、绿色和蓝色值的不透明的 sRGB 颜色
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//禁止页面缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg"); //设置响应正文的MIME类型为图片
int width=60, height=20;
/**创建一个位于缓存中的图像,宽度60,高度20 */
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics(); //获取用于处理图形上下文的对象,相当于画笔
Random random = new Random(); //创建生成随机数的对象
g.setColor(getRandomColor(200,250)); //设置图像的背景色
g.fillRect(0, 0, width, height); //画一个矩形 ,坐标(0,0),宽度60,高度20
g.setFont(new Font("Times New Roman",Font.PLAIN,18)); //设定字体格式
g.setColor(getRandomColor(160,200));
for(int i=0;i<130;i++){ //产生130条随机干扰线
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl); //在图象的坐标(x,y)和坐标(x+x1,y+y1)之间画干扰线
}
String strCode="";
for (int i=0;i<4;i++){
String strNumber=String.valueOf(random.nextInt(10)); //把随机数转换成String字符串
strCode=strCode+strNumber;
//设置字体的颜色
g.setColor(new Color(15+random.nextInt(120),15+random.nextInt(120),15+random.nextInt(120)));
g.drawString(strNumber,13*i+6,16); //将验证码依次画到图像上,坐标(x=13*i+6,y=16)
}
request.getSession().setAttribute("Code",strCode); //把验证码保存到Session中
g.dispose(); //释放此图像的上下文以及它使用的所有系统资源
ImageIO.write(image, "JPEG", response.getOutputStream()); //输出JPEG格式的图像
response.getOutputStream().flush(); //刷新输出流
response.getOutputStream().close(); //关闭输出流
}
}
配置的XML是在上一篇的基础上进行的配置:
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyBlog</display-name>
<servlet>
<servlet-name>RegServlet</servlet-name>
<servlet-class>servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/RegServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ValidateCodeServlet</servlet-name>
<servlet-class>servlet.ValidateCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCodeServlet</servlet-name>
//这里的url将会是后面验证码的src的来源
<url-pattern>/ValidateCodeServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
//在上次的基础上添加了两行代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>府城的博客</title>
</head>
<body>
<form action ="RegServlet" method="Post" onsubmit="return reg(this);">
<table align="center" border="0", width="500">
<tr>
<td align ="right" width ="30%">用户名:</td>
<td><input type ="text" name ="username" class="box"></td>
</tr>
<tr>
<td align ="right" width ="30%">密码:</td>
<td><input type ="text" name ="password" class="box"></td>
</tr>
<tr>
<td align ="right" width ="30%">性别:</td>
<td><input type ="radio" name ="sex" value="男" checked="checked">男
<input type ="radio" name ="sex" value="女" >女</td>
</tr>
<tr>
<td align ="right" width ="30%">密码找回问题:</td>
<td><input type ="text" name ="question" class="box"></td>
</tr>
<tr>
<td align ="right" width ="30%">密码找回答案:</td>
<td><input type ="text" name ="answer" class="box"></td>
</tr>
<tr>
<td align ="right" width ="30%">邮箱:</td>
<td><input type ="text" name ="email" class="box"></td>
</tr>
<tr>
<td>验证码:</td>
<td>
<img alt="" src="ValidateCodeServlet" >
</td>
</tr>
<tr>
<td>输入验证码:</td>
<td>
<input type="text" name="code"/>
</td>
</tr>
<tr>
<td colspan="2" align="center" height="40">
<input type ="submit" value="注册">
<input type ="reset" value="重置">
</td>
</tr>
</body>
</html>
然后就成功了
这里我没有加验证输入的正确性,很简单的逻辑就不说了。以及如何将四位数字验证码变成字母加数字,无非是ASCLL码的范围。