package accp.test.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RandomCodeServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4233448556545944091L;
//验证码图片的宽度和高度
private int width=60;
private int height=20;
/**
* Constructor of the object.
*/
public RandomCodeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage buffImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g=buffImg.createGraphics();
Random random=new Random();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
Font font=new Font("Times New Roman",Font.PLAIN,18);
g.setFont(font);
//画边框
g.setColor(Color.BLACK);
g.drawRect(0, 0, width-1, height-1);
//随机长生160条干扰线,防止被其他程序探测到
g.setColor(Color.GRAY);
for(int i=0;i<160;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1=random.nextInt(12);
int y1=random.nextInt(12);
g.drawLine(x, y, x+x1, y+y1);
}
StringBuffer randomCode=new StringBuffer();
int red=0,green=0,blue=0;
//随机产生4位验证码
for (int i = 0; i < 4; i++) {
String strRand=String.valueOf(random.nextInt(10));
red=random.nextInt(110);
green=random.nextInt(50);
blue=random.nextInt(50);
g.setColor(new Color(red,green,blue));
g.drawString(strRand, 13*i+6, 16);
randomCode.append(strRand);
}
//将4位数字的验证码保存到Session中
HttpSession session=request.getSession();
session.setAttribute("randomCode", randomCode.toString());
//静止图像缓存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
用servlet实现验证码功能
需积分: 10 138 浏览量
2008-09-26
08:56:19
上传
评论
收藏 9KB RAR 举报
ck7355769
- 粉丝: 0
- 资源: 2