这段代码是验证码生成页面的代码,authcode.jsp
<%@ page contentType="image/jpeg" %>
<%@ page import="java.awt.*,java.awt.image.*,java.util.*" %>
<%@ page import="javax.imageio.*,java.io.OutputStream,java.io.IOException" %>
<%@ page pageEncoding="utf-8"%>
<%!
//给定范围获得随机颜色
private 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);
} private String getCertPic(int width, int height, OutputStream os) {
if (width <= 0) width = 60;
if (height <=0) height = 18;
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, 16));
//画边框
g.setColor(getRandColor(200, 250));
g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; 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 mapTable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789"; // 取随机产生的认证码
String sRand = "";
for (int i = 0; i < 4; i++) {
char rand = mapTable.charAt(random.nextInt(mapTable.length()));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(rand), 13 * i + 6, 14 + random.nextInt(5));
}
g.dispose();
try {
ImageIO.write(image, "JPEG", os);
os.close();
} catch (IOException e) {
return "";
}
return sRand;
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String code = getCertPic(60, 18, response.getOutputStream());
session.setAttribute("certCode", code);
//下边两行是防止抛出getOutputStream() has already been called for this response异常的
out.clear();
out = pageContext.pushBody();
%>
这段代码是登录页面引用代码 login.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"><title>欢迎登录</title><meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function changeimg(){
var myimg = document.getElementById("code");
now = new Date();
myimg.src = "<%=path %>/Public/authcode.jsp?code=" + now.getTime();
}
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>

<td>

验证码:<input type="text" id="certCode" name="certCode" maxlength="4"
style="height:18px; width:65px; border:solid 1px #cadcb2; font-size:12px; color:#000;" value='<s:property value="#session.certCode"/>' />
</td>
<td>

 <a style="text-decoration: none;" href="javascript:changeimg()"><img id="code" src="Public/authcode.jsp" style="height:18px; width:64px;"  alt="看不清,换一张" title="看不清,换一张"></a>
</td>
</tr>

</table>
</body>
</html>
问题是打开login.jsp时,验证码不会写入session中,刷新login.jsp页面,验证码会写入,但是是第一次显示的验证码,不同步,如何直接在login页面中输出同步的验证码?尽量避免写后台代码!
谢谢了,实在不行,有后台代码可以解决问题的就告诉我吧,求求了,谢谢啊!验证码sessionJSP

解决方案 »

  1.   

     都不改的,因为你的HTML 是顺序加载的,当你获取SESSION 的验证码的时候,你的请求验证码的请求还未发送。当然你第一次获取的验证码的时候不同步,其实你的SESSIon已经存在了哪个验证码的SESSIon了
                                               from:新华电脑官方总群  群号:73735283
      

  2.   

    你为什么要把验证码分两段写?验证码本来就是一个jsp文件,生成完随机数紧着着就放到session,最后用流一次性生成,你代码写的好乱。页面直接用<img/>标签取就行了么。简单事情。