Servlet验证码源码

来源:互联网 发布:阿里云新手教程 编辑:程序博客网 时间:2024/06/06 07:34

package com.hbsi.csdn.SessionTest;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

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 SessionCheckcode extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public SessionCheckcode() {
  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 {

  this.doPost(request, response);
 }

 /**
  * 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
  */
 private static int WIDTH=60;
 private static int HEIGHT=20;
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("image/jpeg");
  ServletOutputStream sos = response.getOutputStream();
  HttpSession session=request.getSession();
  
  response.setDateHeader("Expires", 0);
  response.setHeader("Cache-Control", "no-Cache");
  response.setHeader("Pragma", "no-Cache");
  
  BufferedImage image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
  Graphics g=image.getGraphics();
  
  //产生随机的验证码
  char[] rands=generateCheckcode();
  //产生图像背景
  drawBackground(g);
  //产生随机数的图片
  drawRands(g,rands);
  g.dispose();
  //将图像输出到客户端
  ByteArrayOutputStream bos=new ByteArrayOutputStream();
  ImageIO.write(image, "JPEG", bos);
  byte[] buf=bos.toByteArray();
  response.setContentLength(buf.length);
  sos.write(buf);
  bos.close();
  sos.close();
  
  session.setAttribute("check_code", new String(rands));
 }

 private void drawRands(Graphics g, char[] rands) {
  g.setColor(Color.black);
  g.setFont(new Font(null,Font.ITALIC|Font.BOLD,18));
  g.drawString(""+rands[0], 1, 17);
  g.drawString(""+rands[1], 16, 16);
  g.drawString(""+rands[2], 31, 15);
  g.drawString(""+rands[3], 46, 18);
  
 }

 private void drawBackground(Graphics g) {
  //设置颜色,画矩形
  g.setColor(new Color(0XDCDCDC));
  g.fillRect(0, 0, WIDTH, HEIGHT);
  //随机产生120干扰点
  for(int i=0;i<120;i++){
   int x=(int) (Math.random()*WIDTH);
   int y=(int) (Math.random()*HEIGHT);
   
   int r=(int) (Math.random()*255);
   int g1=(int) (Math.random()*255);
   int b=(int) (Math.random()*255);
   g.setColor(new Color(r,g1,b));
   g.drawOval(x, y, 1, 0);  
  }
 }

 private char[] generateCheckcode() {
  String chars="0123456789abcdefghijklmnopqrstuvwxyz";
  char []rands=new char[4];
  for(int i=0;i<4;i++){
   int rand=(int) (Math.random()*36);
   rands[i]=chars.charAt(rand);
  }
  return rands;
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

原创粉丝点击