验证码 +session保存获取

3 篇文章 0 订阅
         //获取
	   @RequestMapping("yzm")
	   public  void  yzm(HttpServletRequest re,HttpServletResponse po) throws IOException{
		     int height=45;
		     int width=80;
		     BufferedImage buff=new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);
		     Graphics gr=buff.getGraphics();
		     gr.setColor(Color.white);
		     //字体样式,加粗,大小
             gr.setFont(new Font("微软雅黑",Font.BOLD,18));
             gr.fillRect(0, 0, width, height);//制定矩形
             String zm="0123456789QWERTYUIOPASDFGHJKLZXCVBNM";
             StringBuffer  sb=new StringBuffer();
             Random r=new Random();
             for(int i=0;i<4;i++){
            	 int index=(int) (Math.random()*zm.length());
            	 Color c=new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), r.nextInt(255));
            	 gr.setColor(c);
                 char zms=zm.charAt(index);
             //设置每个字符验证码的位置  x,y
             gr.drawString(""+zms,i*(width/4),(height/2)+5);  
                 sb.append(zms);
             }
             System.out.println(sb.toString());
             // 将四位数字的验证码保存到Session中,用于验证。
              HttpSession session=re.getSession();
              session.setAttribute("code", sb.toString());
             
              // 禁止图像缓存。
            /*  po.setHeader("Pragma", "no-cache");
              po.setHeader("Cache-Control", "no-cache");
              po.setDateHeader("Expires", 0);
              po.setContentType("image/jpeg;charset=utf-8"); */ 

             for(int i=0;i<12;i++){
                 Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
		         gr.setColor(c);
		         gr.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
             }
             // 将图像输出到Servlet输出流中。
             ServletOutputStream sos = po.getOutputStream();
             ImageIO.write(buff, "jpeg",sos);
             sos.close();
	     }
	      
	     
}

   <pre name="code" class="java"> @RequestMapping("code")
	   public void code(HttpServletRequest re,HttpServletResponse po) throws UnsupportedEncodingException{
		   HttpSession session=re.getSession();
           String code=(String) session.getAttribute("code");
           System.out.println("session获取:"+code);
           po.addCookie(new Cookie("code",code));
           //解决中文乱码问题
           
           po.addCookie(new Cookie("code1", URLEncoder.encode("张三", "UTF-8"))); 
           Cookie[] cookies = re.getCookies();
           String  cookieName="";       
           String codes="";
           Integer maxage=0;
           for (Cookie cookie : cookies)
           {
              if(cookie.getName().equals("code1")){
            	  cookieName = URLDecoder.decode(cookie.getValue(), "UTF-8");
              }
              if(cookie.getName().equals("code")){
                //值获取
                codes=cookie.getValue();
                //设置,获取时间
                 maxage=cookie.getMaxAge();
              }
           } 
           System.out.println(cookieName);
           System.out.println(codes);
           System.out.println(maxage); 
           
	   }


 

     
  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值