jsp页面图片验证码获取的session值始终显示前一个解决方案
处理的思想是通过在当前的页面嵌入iframe,在iframe中获取session及相关的脚本处理,当验证码重置的时候iframe同步刷新。这样就保证了获取的session始终与展示的验证码图片一致。
代码如下:
a.jsp
<body>
<div style="left:250px;top:80px;width:300px;height:45px;position:absolute;color:red;" id="warning"></div>
<form action="<%=path %>/GIFT_CARD/giftCardAction!activateGiftCard" method="post" onsubmit="return window.frames['childframe'].giftCard.sub();">
卡号密码:<input type="text" name="cardPsw" id="cardPsw" onkeypress="window.frames['childframe'].giftCard.changeValue()"/><br/>
验证码:<input type="text" name="verifyCode" id="verifyCode"/><img id="imgverify" border="0" src="image.jsp?rad=<%=new java.util.Date() %>" onclick="window.frames['childframe'].giftCard.changeVerifyCode();"/>
<input type="submit" value="提交"/>
</form>
<iframe id="childframe" src="" width="0" height="0"></iframe>
</body>
<script type="text/javascript">
function fn(){
document.getElementById("childframe").src="b.jsp";
}
setTimeout("fn()",1000);
</script>
</html>
b.jsp(iframe页面)
<body>
<script type="text/javascript">
var giftCard = {
changeValue:function (){
var pswvalue = parent.document.getElementById("cardPsw").value;
if(pswvalue.length==2 || pswvalue.length==7 || pswvalue.length==12 || pswvalue.length==17){
parent.document.getElementById("cardPsw").value = pswvalue+" ";
}
},
sub:function (){
var pswvalue = parent.document.getElementById("cardPsw").value;
var verifac = parent.document.getElementById("verifyCode").value;
pswvalue = pswvalue.replace(/[ ]/g,"");
if(pswvalue.length!=18){
parent.document.getElementById("warning").innerHTML="请输入正确的礼品卡密码!";
return false;
}else if(verifac!="<%=request.getSession().getAttribute("imagecode")%>"){
parent.document.getElementById("warning").innerHTML="请输入正确的验证码!";
return false;
}
},
changeVerifyCode:function (){
parent.document.getElementById("imgverify").src="image.jsp?random="+Math.random();
parent.document.getElementById("childframe").src="b.jsp?random="+Math.random();
}
}
</script>
</body>
image.jsp
<%@page import="java.io.InputStream"%>
<%@ page contentType="image/JPEG"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="GBK"%><%!Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}%>
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.flushBuffer();
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 100; 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);
}
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
session.setAttribute("imagecode", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
%>