yanZm.jsp:
<%@ page language="java" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
href="#" onclick="changeImg();">看不清 -------------------------------------
servlet:ImageServlet.java
package com.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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 ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int width=150;//验证码图片宽度
int height=60;//验证码图片高度
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
Graphics g=image.getGraphics();
Random random=new Random();//创建一个随机类
g.setColor(getRandColor(200,250));//背景颜色要偏淡
g.fillRect(0, 0, width, height);//画背景
g.setColor(getRandColor(0,255));//边框颜色
g.drawRect(0, 0, width-1, height-1);//画边框
g.setColor(getRandColor(160,200));// 随机产生5条干扰线,使图象中的认证码不易被其它程序探测到
for(int i=0;i<8;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1=random.nextInt(width);
int y1=random.nextInt(height);
g.drawLine(x, y, x1, y1);
}
g.setColor(getRandColor(160,200));// 随机产生100点,使图象中的认证码不易被其它
程序探测到
for(int i=0;i<100;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
g.drawLine(x, y, x, y);
}
Font font = new Font("Times New Roman", Font.ITALIC,38); /
/ 创建字体,字体的大小应该根据图片的高度来定。
g.setFont(font);//设置字体
int length = 6; // 设置默认生成4个验证码
String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 设置备选验证码:包括"a-z"和数字"0-9"
String sRand="";
// 用随机产生的颜色将验证码绘制到图像中。
// 生成随机颜色(因为是做前景,所以偏深)
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
for(int i=0;i
String ch=String .valueOf(s.charAt(random.nextInt(s.length())));
sRand+=ch;
g.drawString(ch, 22*i+12, (random.nextInt(5)-2)*i+40);
}
//将生成的字符串存储在session中
HttpSession session=request.getSession();
s