在jsp页面上用EL表达式获取验证码类里存的验证码的值
HttpSession session = request.getSession(true);
session.setAttribute("rand", sRand);
JSP页面上${rand},第一次加载能看到验证码,但得不到值,第二次刷新,才能看到第一次验证码的值
EL表达式每次只能取到上一次验证码的值?请问怎么解决这个问题

解决方案 »

  1.   

    跟el没关系吧
    session是有生命周期的
    要注意这个
      

  2.   

    估计是action中scope作用范围的问题
      

  3.   

    我页面是JSP写的  img标签里src写的是那个验证码的类 我怀疑是不是这里逻辑出错了?
    服务器编译时就执行了一次?怎么改呢
      

  4.   

    package com.util;import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Random;import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;public class GetImg extends HttpServlet {

    private Font mFont = new Font("Times New Roman", Font.PLAIN, 17); /**
     * 产生随机颜色
     * @param fc
     * @param bc
     * @return
     */
    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);
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //禁止客户端浏览器缓存
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 表明生成的响应是图片
    response.setContentType("image/jpeg"); //在内存缓冲区中创建图片
    int width = 90, 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(1, 1, width - 1, height - 1);
    g.setColor(new Color(102, 102, 102));
    g.drawRect(0, 0, width - 1, height - 1);
    g.setFont(mFont); g.setColor(getRandColor(160, 200)); // 画随机线
    for (int i = 0; i < 155; i++) {
    int x = random.nextInt(width - 1);
    int y = random.nextInt(height - 1);
    int xl = random.nextInt(6) + 1;
    int yl = random.nextInt(12) + 1;
    g.drawLine(x, y, x + xl, y + yl);
    } // 从另一方向画随机线
    for (int i = 0; i < 70; i++) {
    int x = random.nextInt(width - 1);
    int y = random.nextInt(height - 1);
    int xl = random.nextInt(12) + 1;
    int yl = random.nextInt(6) + 1;
    g.drawLine(x, y, x - xl, y - yl);
    }

    // 生成随机数,并将随机数字转换为字母
    String sRand = "";
    for (int i = 0; i < 4; i++) {
    int itmp = random.nextInt(26) + 65;
    char ctmp = (char) itmp;
    sRand += String.valueOf(ctmp);
    g.setColor(new Color(20 + random.nextInt(110), 20 + random
    .nextInt(110), 20 + random.nextInt(110)));
    g.drawString(String.valueOf(ctmp), 20 * i + 10, 18);
    }

    //将验证码保存至Session
    HttpSession session = request.getSession(true);
    session.setAttribute("rand", sRand);
    g.dispose();
    //输出到客户端
    ImageIO.write(image, "JPEG", response.getOutputStream());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    }}一下是页面
      

  5.   

      <body>
       <form action="register.do" method="post">
    <TABLE width=60% cellpadding=0  align="center">
    <TR>
    <TD><a href="index.jsp">xxx论坛</a>>>注册</TD>
    </TR>
         <TR>
    <TD>
         <TABLE rules=rows border=0 cellspacing=1 cellpadding=4 class="bg" width="100%" height="100%" > <!--嵌套表格 -->
         <TR>
    <TD colspan=2><B>注册</B></TD>

    </TR>
         <TR style="background:#FFFFCC;">
    <TD style="color:#99CC00;" colspan=2><B>必填</B></TD>
    </TR>
    <TR>
    <TD style="color:#000000;" colspan=2 align="center"><span id="inputinfo">注册信息</span></TD>
    </TR>
         <TR class="tr">
    <TD  width=200x ><B>用户名</B></TD>
    <TD><input type="text" name="username" id="username"/> 
    <span id="usernametx"></span>
    </TD>
         </TR>
        <TR class="tr">
    <TD><B>密码</B></TD>
    <TD><input type="password" name="password" id="pwd1">
    <span id="pwd1tx" ></span>
    </TD>
         </TR>
         <TR class="tr">
    <TD><B>确认密码</B></TD>
    <TD>
    <input type="password"  style="float:left;" id="pwd2">
    <span id="pwd2tx"></span>
    </TD>
         </TR>
         <TR class="tr">
    <TD><B>性别</B></TD>
    <TD>
    <input type="radio" name="userSex" value="1" checked="checked">男
    <input type="radio" name="userSex" value="2">女
    </TD>
         </TR>
         <TR class="tr">
    <TD><B>Email</B></TD>
    <TD>
    <input type="text" style="float:left"" name="userEmail" id="userEmail" value="@">
    <span id="emailtx"></span>
    </TD>
         </TR>
         <TR class="tr">
    <TD><B>验证码:<img src="GetImg" alt="图形验证码" onclick="this.src='GetImg?'+new Date().getTime()"; ></img></B></TD>
    <TD><input style="float:left" type="text" id="ValidType" name="ValidType">
    <input type="text" value="${sessionScope.rand}">
    <span id="imgtx"></span>
    </TD>
         </TR>
        </TABLE>
    </TD>
        </TR>
        <TR>
    <TD>
      <TABLE cellspacing=1 border=0 class="bg" width="100%" height="100%">
        <TR class="tr">
    <TD height=30px align="center" >
    <input class="btn" type="submit" value="注    册" disabled="disabled" id="sub">
    <input class="btn" type="reset" value="重    置">

    </TD>

        </TR>
        </TABLE>
    </TD>
        </TR>
        </TABLE>
        </form>
    </body>
      

  6.   

    页面是JSP。我想用el表达式在前台取到值 验证好了  再提交服务器
      

  7.   

    <input type="text" value="${sessionScope.rand}"> 处一般都是用一个servlet-mapping 连接就ok了