我们知道在我们注册用户的时候一般都有一段模糊的验证码让我们输入,其实我们自己也可以实现这个验证码 ,验证码的好处是为了防止某些自动提交软件的而已行为 。
下面就利用Servlet +JSP+JavaBran实现一个验证码机制。
BufferedImage 可以操作缓冲区的 内部Image,可以被ImageIO输出到输出流中 ,我们就是利用PrintWriter可以想浏览器输出信息的原理我们输出Image图片 。
1、产生验证码的类 MakePicture
package me.test;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Random;
import java.io.OutputStream;
import java.io.IOException;
public class MakePicture //产生识别验证图像
{
private char charTable[]={
'a','A','b','B','c','C','d','D' ,'e','E' ,
'f','F','g','G','h','H','i','I','j','J' ,
'0','1','2','3','4','5','6','7','8','9'
};
public String drawPicture(int width,int height,OutputStream os)
{
if(width<=0)
width=100 ;
if(height<=0)
height=60 ;
BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB) ;
Graphics g=image.getGraphics() ;
g.setColor(Color.LIGHT_GRAY) ;
g.fillRect(0, 0, width, height) ;
g.setColor(new Color(0x5265fd)) ;
g.drawRect(0, 0, width, height) ;
String str ="" ;
for(int x=0;x<4;x++)
{
str+=charTable[(int) (Math.random()*charTable.length)];
}
g.drawString(str.substring(0, 1), 0, 15);
g.drawString(str.substring(1, 2), 15, 17);
g.drawString(str.substring(2, 3), 35, 19);
g.drawString(str.substring(3, 4), 50, 16);
Random rand=new Random() ;
for(int i=0;i<10;i++)
{
int x=rand.nextInt(width) ;
int y=rand.nextInt(height) ;
g.drawOval(x, y, 1, 1) ;
}
g.dispose() ;
try {
ImageIO.write(image, "JPEG",os) ;
} catch (IOException e) {
e.printStackTrace();
return "" ;
}
return str ;
}
}
2、index.jsp首页 这个页面通过请求Servlet输出验证码
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=ISO-8859-1">
<title>图片验证</title>
</head>
<body>
<form action="yanzheng.jsp" method="get">
验证码: <input type="text" name="code" />
<img src="show"><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
3、yanzheng.jsp 验证输入的字符是否正确
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.print(request.getParameter("code")) ;
%>
</body>
</html>
4、实现http请求的Servlet实现类 ImageServlet
package me.test;
import java.io.IOException;
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;
public class ImageServlet extends HttpServlet
{
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
MakePicture mp=new MakePicture() ;
String str=mp.drawPicture(60, 20,res.getOutputStream() ) ;
req.getSession().setAttribute("pic", str) ;
res.getOutputStream().print(str) ;
}
}