我做了个登录界面login.jsp,用session获取Servlet中产生的验证码,打印出来一看,却是上一次产生的验证码
第一次打开login.jsp时,session取出的值是null;刷新一下页面,这时取出的值是上一次生成的验证码!
session取出的值都是上一次的值!这样无法达到验证的效果,肯请帮忙!谢谢!附代码!
这是Servlet的service方法,用于产生随机验证码!并将验证码放入Session中protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException { // 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics(); // 创建一个随机数生成器类
Random random = new Random(); // 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height); // 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 设置字体。
g.setFont(font); // 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1); // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
/*g.setColor(Color.BLACK);
for (int i = 0; i < 10; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}*/ // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0; // 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255); // 用随机产生的颜色将验证码绘制到图像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i ) * x+5, codeY); // 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
// 将四位数字的验证码保存到Session中。
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); // 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}以下是jsp代码,在javascript代码段中取出session中的值
<%@ page language="java" import="java.util.*,com.sino.validate.*" pageEncoding="gb2312"%>
<html>
<head>
<title>登录</title>
<link rel="stylesheet" href="images/style.css" type="text/css">
</head>
<body>
<form name="main" action="checklogin.jsp" method="post" onsubmit="return checkSave();">
<table width="100%" border="0">
<tr>
<td><img src="images/login.gif"></img></td>
</tr>
</table>
<table width="30%" border="0" align="center">
<tr align="center">
<td></td>
<td align="left">
用户登录
</td>
</tr>
<tr align="center">
<td align="right" width="50">
用户名:
</td>
<td align="left">
<input type="text" name="id" />
</td>
</tr>
<tr align="center">
<td align="right" width="50">
密 码:
</td>
<td align="left">
<input type="password" name="password" />
</td>
</tr>
<tr align="center">
<td align="right" width="50">
验证码:
</td>
<td align="left">
<input type="text" name="validateCode"/>
</td>
<td align="left">
<img src="validateCodeServlet"/>
</td>
</tr>
<tr>
<td></td>
<td align="left">
<input type="button" value="确 定" onClick="checkSave();"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
function checkSave() {
if (document.main.id.value.length ==0) {
alert("用户名不能为空,请填写!");
document.main.id.focus;
return false;
} else if (document.main.password.value.length == 0) {
alert("密码不能为空,请填写!");
document.main.password.focus;
return false;
}else if(document.main.password.value.length !="<%=session.getAttribute("validateCode")%>" ){
alert("验证码错误,请重新填写!");
document.main.validateCode.focus;
return false;
}
document.main.submit();
}
</script>
第一次打开login.jsp时,session取出的值是null;刷新一下页面,这时取出的值是上一次生成的验证码!
session取出的值都是上一次的值!这样无法达到验证的效果,肯请帮忙!谢谢!附代码!
这是Servlet的service方法,用于产生随机验证码!并将验证码放入Session中protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException { // 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics(); // 创建一个随机数生成器类
Random random = new Random(); // 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height); // 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 设置字体。
g.setFont(font); // 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1); // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
/*g.setColor(Color.BLACK);
for (int i = 0; i < 10; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}*/ // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0; // 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255); // 用随机产生的颜色将验证码绘制到图像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i ) * x+5, codeY); // 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
// 将四位数字的验证码保存到Session中。
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); // 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}以下是jsp代码,在javascript代码段中取出session中的值
<%@ page language="java" import="java.util.*,com.sino.validate.*" pageEncoding="gb2312"%>
<html>
<head>
<title>登录</title>
<link rel="stylesheet" href="images/style.css" type="text/css">
</head>
<body>
<form name="main" action="checklogin.jsp" method="post" onsubmit="return checkSave();">
<table width="100%" border="0">
<tr>
<td><img src="images/login.gif"></img></td>
</tr>
</table>
<table width="30%" border="0" align="center">
<tr align="center">
<td></td>
<td align="left">
用户登录
</td>
</tr>
<tr align="center">
<td align="right" width="50">
用户名:
</td>
<td align="left">
<input type="text" name="id" />
</td>
</tr>
<tr align="center">
<td align="right" width="50">
密 码:
</td>
<td align="left">
<input type="password" name="password" />
</td>
</tr>
<tr align="center">
<td align="right" width="50">
验证码:
</td>
<td align="left">
<input type="text" name="validateCode"/>
</td>
<td align="left">
<img src="validateCodeServlet"/>
</td>
</tr>
<tr>
<td></td>
<td align="left">
<input type="button" value="确 定" onClick="checkSave();"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
function checkSave() {
if (document.main.id.value.length ==0) {
alert("用户名不能为空,请填写!");
document.main.id.focus;
return false;
} else if (document.main.password.value.length == 0) {
alert("密码不能为空,请填写!");
document.main.password.focus;
return false;
}else if(document.main.password.value.length !="<%=session.getAttribute("validateCode")%>" ){
alert("验证码错误,请重新填写!");
document.main.validateCode.focus;
return false;
}
document.main.submit();
}
</script>
解决方案 »
- Mybatis连接Oracle主键自增的问题
- mysql里存的中文可以在页面上显示,但是不能用sql语句直接查询出来是怎么回事?
- 请问这么在页面上做一个记事本。
- 请大学进来帮帮忙,STRUTS怎样配置
- WEB网页中可同时显示图片,文字等HTML内容以及EXCEL表格或者MS WORD内容吗?就是说网页中有一块区别是WORD文档或者EXCEL表格。这种效果如
- JSP调用Javabean,初学者,看了很多文章还是没调试好,请大虾帮忙.
- java+oracle+weblogic开发政府部门业务管理系统!
- 求csv文件导入数据库急在线等,谢谢!
- 写了一个最基本的servlet,编译通不过
- rs.getString("zx01")获得字段值的问题
- 关于JSValidation(验证框架)
- jsp 打印 table边框宽度 问题
session.removeAttribute("check_code");
一样用servlet的输出流做
或者用ajax
<input type="text" name="validateCode"/>
</td>
<td align="left">
<img src="validateCodeServlet"/>
</td>
两个换换位置看看
在过滤器里吧验证码放进session
让img的src随时变化,避免出现浏览器缓存不去请求后台的情况呗。
http://blog.csdn.net/gjd111686/archive/2004/07/08/37113.aspxservlet生成的话确实有时候问题我怀疑你重写service方法-不对重写一个方法看看
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);//设置缓存时间
不过在反复的思考之后解决掉了..以下的描述如果有什么不清楚的,可以留言给我..你要先了解http是怎么完成一次请求的,请求的时候对于不同的内容请求的顺序
或者简单一点说,有没有发现,在一个含有图片的页面上面,当状态栏显示完成的时候,其实图片都没有下载完成
这是因为浏览器把如src 这种标签不是同一次请求服务器获得的,它会将其它的数据请求完毕,然后会在请求所得的数据里面获取src的地址再去请求该地址一次我想说到这里可能已经明白了,你在页面显示的验证码跟此时session里的验证码肯定不是同步的
第一次的时候验证码肯定为空,因为在页面请求完了之后你的码证码此时还未生成呢,等到http再去请求src里的地址生成码证码的图片的时候才会生成验证码,而此时你的页面将session 中的验证码用javascript代码早已生成,但实际上,你的session里面的值因为又一次的请求了servlet已经更新了!
<%
StringBuffer randomCode = new StringBuffer();
Random random = new Random();
for (int i = 0; i < 4; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(random.nextInt(9));
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
//red = random.nextInt(255);
//green = random.nextInt(255);
//blue = random.nextInt(255); // 用随机产生的颜色将验证码绘制到图像中。
//g.setColor(new Color(red, green, blue));
//int codeY = 50;
//int x = 20;
//g.drawString(strRand, (i) * x + 5, codeY); // 将产生的四个随机数组合在一起。
randomCode.append(strRand);
session.setAttribute("validateCode", randomCode.toString());
}
%>
<html>
<head>
<title>登录</title>
<link rel="stylesheet" href="images/style.css" type="text/css">
</head>
<body>
<form name="main" action="checklogin.jsp" method="post"
onsubmit="return checkSave();">
<table width="100%" border="0">
<tr>
<td>
<img src="images/login.gif"></img>
</td>
</tr>
</table>
<table width="30%" border="0" align="center">
<tr align="center">
<td></td>
<td align="left">
用户登录
</td>
</tr>
<tr align="center">
<td align="right" width="50">
用户名:
</td>
<td align="left">
<input type="text" name="id" />
</td>
</tr>
<tr align="center">
<td align="right" width="50">
密 码:
</td>
<td align="left">
<input type="password" name="password" />
</td>
</tr>
<tr align="center">
<td align="right" width="50">
验证码:
</td>
<td align="left">
<input type="text" name="validateCode" />
</td>
<td align="left">
<img src="ddddddd" />
</td>
</tr>
<tr>
<td></td>
<td align="left">
<input type="button" value="确 定" onClick="checkSave();" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%=session.getAttribute("validateCode")%>
<script language="javascript">
function checkSave() {
if (document.main.id.value.length ==0) {
alert("用户名不能为空,请填写!");
document.main.id.focus;
return false;
} else if (document.main.password.value.length == 0) {
alert("密码不能为空,请填写!");
document.main.password.focus;
return false;
}else if(document.main.password.value.length !="<%=session.getAttribute("validateCode")%>" ){
alert("验证码错误,请重新填写!");
document.main.validateCode.focus;
return false;
}
document.main.submit();
}
</script>public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException { int height = 100;
int width = 150;
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics(); // 创建一个随机数生成器类
Random random = new Random(); // 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height); int fontHeight = 20;
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// 设置字体。
g.setFont(font); // 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1); // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
// g.setColor(Color.BLACK);
// for (int i = 0; i < 160; i++) {
// int x = random.nextInt(width);
// int y = random.nextInt(height);
// int xl = random.nextInt(12);
// int yl = random.nextInt(12);
// g.drawLine(x, y, x + xl, y + yl);
// }
// // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0; int codeCount = 4;
//int codeSequence[] = null;
// 随机产生codeCount数字的验证码。
// for (int i = 0; i < codeCount; i++) {
// // 得到随机产生的验证码数字。
// String strRand = String.valueOf(random.nextInt(9));
// // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
// red = random.nextInt(255);
// green = random.nextInt(255);
// blue = random.nextInt(255);
//
// // 用随机产生的颜色将验证码绘制到图像中。
// g.setColor(new Color(red, green, blue));
// int codeY = 50;
// int x = 20;
// g.drawString(strRand, (i) * x + 5, codeY);
//
// // 将产生的四个随机数组合在一起。
// randomCode.append(strRand);
// }
// 将四位数字的验证码保存到Session中。
HttpSession session = req.getSession(true);
g.drawString((String) session.getAttribute("validateCode"), 3 * 20 + 5, 50);
//session.setAttribute("validateCode", randomCode.toString());
System.out.println(String.valueOf((session.getAttribute("validateCode"))));
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); // 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream(); ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}
把验证码放在JSP页面生成。传入servlet.然后由servlet生成图片输出