JSP实现验证码

来源:岁月联盟 编辑:exp 时间:2011-11-13

 

验证码的实现分为JSP页面和Java类Servlet两部分:

JSP页面:

<!DOCTYPE html>

<html>

  <head>

    <title>loginForm.html</title>

 

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="this is my page">

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

   

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

 

  <body>

  <h3>带有验证码的登陆页面</h3>

  <form method="post" action="login.yan" >

    用户名:<input type="text" name="usreName" size="10"><br><br/>

    密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" size="10"><br><br>

    验证码:<input type="text" name="userNum" size="4"><img src="loginForm"/><br/><br/>

    <input type="submit" value="提交">

  </form>

  </body>

</html>

 

Java类:

package com.csdn.session;

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

 private static final long serialVersionUID = 1L;

 public void doGet(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

  doPost(request, response);

 }

 

 private static int WIDTH = 80;

 private static int HEIGHT = 20;

 public void doPost(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

  response.setContentType("image/jpeg");

 

  ServletOutputStream out=response.getOutputStream();

 

  //PrintWriter out = response.getWriter();

  response.setHeader("Pragma", "no-cache");

  response.setHeader("Cache-Controll", "no-cache");

  response.setIntHeader("Expires",0);

  //背景

 

  BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);

  Graphics g = bi.getGraphics();

  drawBackground(g);

 

  //随机产生验证码

  char[] rands=generateCheckCode();

 

  //System.out.println(fun(rands));

 

  HttpSession session=request.getSession();

 

  session.setAttribute("num",fun(rands)+"");//验证码==12-9=3;写入sesion对象

 

  drawRands(g,rands);

 

  ByteArrayOutputStream bos=new ByteArrayOutputStream();

 

  ImageIO.write(bi, "JPEG", bos);

 

  byte[] buf=bos.toByteArray();//将bi对象里面内容转化成字符

 

  out.write(buf);

 

  response.setContentLength(buf.length);

 

  out.flush();

  out.close();

 }

 

 

 private int fun(char[] rands){

 

  int num1=Integer.parseInt(rands[0]+"")*10+Integer.parseInt(rands[1]+"");

  int num2=Integer.parseInt(rands[2]+"");

  int num=0;

  if(rands[2]=='+'){

   num=num1+num2;

  }else{

    num=num1-num2;

  }

 

  return num;

 

 }

 

 

 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,20);

  g.drawString(" "+rands[1],14,17);

  g.drawString(" "+rands[3],30,16);

  g.drawString(" "+rands[2],42,18);

  g.drawString(" "+rands[4],56,16);

 

 }

 

 private char[] generateCheckCode() {

 

  String chars="0123456789";

  String char1="+-";

  char[] rands=new char[5];

  rands[0]=chars.charAt((int) (Math.random()*2)+1);

  rands[1]=chars.charAt((int) (Math.random()*10));

  rands[2]=chars.charAt((int) (Math.random()*9)+1);

  rands[3]=char1.charAt((int) (Math.random()*2));

  rands[4]='=';

  return rands;

 }

 

 private void drawBackground(Graphics g) {

  g.setColor(new Color(0XDCDCDC));

 

  g.fillRect(0,0,WIDTH,HEIGHT);

 

  for(int i=0;i<20;i++){

   int x=(int) (Math.random()*WIDTH);

   int y=(int) (Math.random()*HEIGHT);

  

   int red=(int) (Math.random()*255);

   int greed=(int) (Math.random()*255);

   int blue=(int) (Math.random()*255);

  

   g.setColor(new Color(red,greed,blue));

   g.drawOval(x, y, 1, 0);

  

  }

 

 }

}

  摘自 宋利兴的专栏