驗證碼的實現分為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/>
密 碼:<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);
}
}
}
摘自 宋利興的專欄