我做了个登录界面login.jsp,用session获取Servlet中产生的验证码,打印出来一看,却是上一次产生的验证码
第一次打开login.jsp时,session取出的值是null;刷新一下页面,这时取出的值是上一次生成的验证码!
session取出的值都是上一次的值!这样无法达到验证的效果,肯请帮忙!谢谢!附代码!
这是Servlet的service方法,用于产生随机验证码!并将验证码放入Session中
package com.fk.util;
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 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;
/**
* 获得图片验证码
* @author Administrator
*
*/
public class GetImageCode extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int WIDTH = 60;
private static int HEIGHT = 20;
public 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;
}
public 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);