在做用户登录的时候,为了安全起见,都会在登录按钮前设置一个验证码输入,下面是一个验证码输入的函数,用servlet开发的。
package com.wy.servlet;
import java.awt.*;
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;
import javax.servlet.http.HttpSession;
public class PictureCheckCode extends HttpServlet {
private Font mFont = new Font("黑体", Font.BOLD, 17);
public PictureCheckCode() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void init() throws ServletException {
// Put your code here
super.init();
}
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);
// 指定生成的响应是图片
response.setContentType("image/jpeg");
int width = 70;
int height = 19;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
// 画随机的线条
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(6) + 1;
int y1 = random.nextInt(12) + 1;
g.drawLine(x, y, x + x1, y + y1);
}
String sRand = "";
// 输出随机的验证文字
for (int i = 0; i < 4; i++) {
int itmp = random.nextInt(26) + 65;
char ctmp = (char) itmp;
sRand += String.valueOf(ctmp);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);
}
// 将生成的验证码保存到Session中
HttpSession session = request.getSession(true);
session.setAttribute("rand", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
}
另外,需要在web.xml中进行配置,配置如下:
<servlet>
<servlet-name>PictureCheckCode</servlet-name>
<servlet-class>com.wy.servlet.PictureCheckCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PictureCheckCode</servlet-name>
<url-pattern>/PictureCheckCode</url-pattern>
</servlet-mapping>
然后,就可以在网页中使用了,使用方法如下:
<%@ 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>
<div align="center">
<form action="userInfo?method=5" method="post">
<table width="367" border="0">
<tr>
<td><img border=0 src="PictureCheckCode" name="checkCode"></td>
<td width="163" colspan="2"><input name="code" type="text"></td>
</tr>
<tr>
<td height="25"> </td>
<td><input type="submit" name="Submit" value=" 提 交 "></td>
</tr>
<tr>
<td height="30"> </td>
<td colspan="3">${requestScope.information}</td>
</tr>
</table>
</form>
</div>
</body>
</html>
生成的效果如下:
验证方法如下:
private void checkPictureCode(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
String information="";
session = request.getSession();
String rand = (String) session.getAttribute("rand");
String code = (String) request.getParameter("code");
if (rand.equals(code)) {
information = "你输入的验证码正确";
} else {
information = "您输入的验证码不正确!";
}
request.setAttribute("information", information);
request.getRequestDispatcher("checkPictureCode.jsp").forward(request, response);
}
大致的流程就是这些。