以下是Servlet产生验证码:
package com.web;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
//import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
*@package:com.verifacationCode
*@name:VerificationCodeServlet.java
*@Dscription:处理验证码
*@author: hngd-DQ-zy
*@Date:2014-8-14 下午2:40:51
*/
public class VerificationCode extends HttpServlet {
private static final long serialVersionUID = 1L;
private final int WIDTH=150;
private final int HEIGHT=30;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//准备画布
BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
//准备画笔
Graphics g=image.getGraphics();
//设置背景颜色
setBackground(g);
//设置边框
setBorder(g);
//画干扰线
drawRandomLine(g);
//画文字
drawRandomNum((Graphics2D)g);
//设置浏览器不要缓存
response.setContentType("image/jpeg");
response.setDateHeader("expries",-1);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());
}
private void setBackground(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
private void setBorder(Graphics g){
g.setColor(Color.BLUE);
g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
}
private void drawRandomLine(Graphics g){
g.setColor(Color.GREEN);
for(int i=0;i<8;i++){
int x1=new Random().nextInt(WIDTH);
int y1=new Random().nextInt(HEIGHT);
int x2=new Random().nextInt(WIDTH);
int y2=new Random().nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
}
private void drawRandomNum(Graphics2D g){
//StringBuffer s=new StringBuffer();
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,20));
String base="1234567890ABCDEFGHJKLMNOPQRSTUVWXYZ";
int x=20;
for(int i=0;i<4;i++){
//随机产生旋转角度(以度为单位且小于40度)
int degree=new Random().nextInt()%40;
//在提前准备好的字符串中随机选择一个字符
String ch=base.charAt(new Random().nextInt(base.length()-1))+"";
//设置选择弧度和基点
g.rotate(degree*(Math.PI/180), x, 20);
g.drawString(ch, x, 25);
//第一个字旋转完毕后第二个字要还原
g.rotate(-degree * Math.PI / 180, x, 20);
x+=30;
}
}
以下是jsp中嵌入验证码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function newimg(img){
img.src=img.src+"?"+new Date().getTime();
}
</script>
<style type="text/css">
#id-1{
cursor: pointer;
}
</style>
</head>
<body>
<form>
<img id="img-1" src="VerificationCode" οnclick="newimg(this)" />
<input id="id-1" type="button" value="刷新" />
</form>
</body>
</html>