判断验证码输入是否正确问题
我做了个登录界面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);
g.drawString("" + rands[1], 16, 15);
g.drawString("" + rands[2], 31, 18);
g.drawString("" + rands[3], 46, 16);
}
public void drawBackground(Graphics g) {
// 画背景
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, WIDTH, HEIGHT);
// 随机产生120个干扰点
for (int i = 0; i < 120; i++) {
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
g.setColor(new Color(red, green, blue));
g.drawOval(x, y, 1, 0);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
// 设置浏览器不缓存此图片
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 创建内存图像并获得其图形上下文
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 产生随机的验证码
char[] rands = generateCheckCode();
// 产生图像
drawBackground(g);
drawRands(g, rands);
// 结束图像的绘制过程,完成图像
g.dispose();
// 将图像输出到客户端
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", bos);
byte[] buf = bos.toByteArray();
response.setContentLength(buf.length);
sos.write(buf);
bos.close();
sos.close();
// 将当前验证码存入到session中
session.setAttribute("checkCode", new String(rands));
System.out.println(session.getAttribute("checkCode"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<servlet>
<servlet-name>GetImageCode</servlet-name>
<servlet-class>com.fk.util.GetImageCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetImageCode</servlet-name>
<url-pattern>/GetImageCode</url-pattern>
</servlet-mapping>
<div class="control-group">验证码 js判断验证码
<label class="control-label">*验证码:</label>
<div class="controls" style="width: 350px;">
<input type="text" name="verifyCode" id="verifyCode" maxlength="20" />
<div id="code_div">
</div>
<div id="verifyCode_error"></div>
</div>
</div>
var bol5=false;
/**
* emailReg js验证
* V1.0
*/
$(function (){
var i=0;
//验证码
$("#verifyCode").blur(function(){
var verifyCode=$("#verifyCode").val();
if(i==0){
$("#code_div").html("<img alt="验证码" id="safecode" src="${ctx}/GetImageCode">"+"<a href="javascript:changeCode();" id="changCode">看不清楚</a>");
i++;
}
if(verifyCode.replace(/(^\s*)|(\s*$)/g,"")==""){
bol5=false;
$("#verifyCode_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' width="15px" height="15px"/>请输入验证码");
}else{
$.ajax({
type: "post",
url: "${ctx}/user.do?method=checkImgCode&imgCode="+verifyCode,
dataType: "json", /*这句可用可不用,没有影响*/
contentType: "application/json; charset=utf-8",
success: function (bol) {
if(!bol){
bol5=true;
$("#verifyCode_error").html("<img src='${ctx}/images/success_img.gif' width='16px' height='16px'/>");
}else{
bol5=false;
$("#verifyCode_error").attr("class","error_div").html("<img src='${ctx}/images/error_img2.gif' width='15px' height='15px'/>"+"验证码不正确");
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
});
});
[解决办法]
你页面应该有个<img src="GetImageCode/>标签啊,不然怎么显示验证码图片
[解决办法]
在你的页面
<label class="control-label">*验证码:</label>
<div class="controls" style="width: 350px;">
<input type="text" name="verifyCode" id="verifyCode" maxlength="20" />
<div id="code_div">
</div>
<div id="verifyCode_error"></div>
</div>
</div>
的
<div id="code_div"> </div>
改为<image id="code_div" src="GetImageCode" >
然后后面的js验证不要了
重新生成一个Servlet 获取页面 <input type="text" name="verifyCode" id="verifyCode" maxlength="20" /> 的${verifyCode}值 与getsession.getAttribute("checkCode")值比较久可以了!相同就直接跳转ok了
[解决办法]
你还做了个异步验证啊,这么感觉你问的问题和你说已解决的答案,不一样呢?
[解决办法]
避免缓存,url中传一个随机数random