首页 新闻 会员 周边

验证码刷新后session当中保存的是上次的值!!!

0
悬赏园豆:15 [待解决问题]

验证码刷新后session当中保存的是上次的值!!!

LRyy的主页 LRyy | 初学一级 | 园豆:154
提问于:2017-10-02 22:09
< >
分享
所有回答(2)
0

贴代码

MIMU86 | 园豆:200 (初学一级) | 2017-10-03 00:19
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ page contentType="image/jpeg"  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>  
  3. <%!  
  4.     /** 
  5.     * 生成随机着色 
  6.     */  
  7.     public Color getColor(){  
  8.         Random random = new Random();  
  9.         int r = random.nextInt(256);//0-255  
  10.         int g = random.nextInt(256);  
  11.         int b = random.nextInt(256);  
  12.         return new Color(r,g,b);  
  13.     }  
  14.       
  15.     //定义验证码的随机字符串  
  16.     String randomStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";  
  17.       
  18.     /** 
  19.     * 生成随机字符 
  20.     */  
  21.     public String getStr(){  
  22.         StringBuffer sb = new StringBuffer();  
  23.         Random random = new Random();  
  24.         for(int i=0;i<6;i++){  
  25.             sb.append(randomStr.charAt(random.nextInt(randomStr.length())));//每次提取一个随机字符  
  26.         }  
  27.         return sb.toString();  
  28.     }  
  29. %>  
  30. <%  
  31.     //设置响应头部信息告诉浏览器不缓存图片  
  32.     response.setHeader("pragma", "mo-cache");  
  33.     response.setHeader("cache-control", "no-cache");  
  34.     response.setDateHeader("expires", 0);  
  35.        
  36.     BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);  
  37.        
  38.     Graphics g = image.getGraphics();  
  39.     g.setColor(new Color(200,200,200));  
  40.     g.fillRect(0,0,100,30);  
  41.       
  42.     Random random = new Random();   
  43.     //绘制40条干扰线   
  44.     for (int i = 0; i < 40; i++) {  
  45.          
  46.         int x = random.nextInt(100);  
  47.         int y = random.nextInt(30);  
  48.         int xl = random.nextInt(x+10);  
  49.         int yl = random.nextInt(y+10);  
  50.         g.setColor(getColor());  
  51.         g.drawLine(x, y, x + xl, y + yl);  
  52.     }  
  53.        
  54.        
  55.     g.setFont(new Font("宋体", Font.ROMAN_BASELINE,24));  
  56.     g.setColor(Color.BLACK);  
  57.     String randomCode = getStr();  
  58.        
  59.     StringBuffer sb = new StringBuffer();  
  60.     for(int i=0;i<randomCode.length();i++){  
  61.         g.drawString(randomCode.charAt(i)+"",random.nextInt(10),20+random.nextInt(8));//sb.append(randomCode.charAt(i));  
  62.         g.translate(15,0);  
  63.     }  
  64.        
  65.     //将验证码放入Session中  
  66.     session.setAttribute("RANDOMCODE",randomCode);  
  67.       
  68.     //通过流将验证码图片输出   
  69.     ImageIO.write(image,"jpeg",response.getOutputStream());  
  70.     out.clear();  
  71.     out = pageContext.pushBody();  
  72. %>  

String rightCheckCode = (String)request.getSession().getValue("RANDOMCODE"); 

支持(0) 反对(0) LRyy | 园豆:154 (初学一级) | 2017-10-04 09:24
0

那应该就是你的代码逻辑有问题

trustme326 | 园豆:391 (菜鸟二级) | 2017-10-03 10:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册