模拟后台接收短信验证码
2016-05-24 13:07
495 查看
struts2 Action中
//模拟生成6为随机验证码
public String createCode(){ String code=null; Random random=new Random(); int[] rand=new int[6]; int randInt = 0; String randStr=""; String temp; for(int i=0;i<6;i++){ randInt = random.nextInt(10); rand[i]=randInt; temp=rand[i]+""; randStr=randStr+temp; } code=randStr; sessionCode.setAttribute("code", code); sessionCode.setAttribute("time", System.currentTimeMillis()); System.out.println(code); return null; }
//首先判断会话是否获取短信验证码
//设置验证码失效时间,通过System.currentTimeMillis();获取访问各自接口的毫秒数,做差与30000(ms)作比较,超时移除。
//其中(30000ms)为本测试接口设置
//的短信验证码生效时间,向页面输入的验证码"codeInput="与获取的验证码进行比较
private static Logger logger = Logger.getLogger ( XxAction.class.getName () );
public String testSession(){ if(sessionCode.getAttribute("code")!=null){ long early=(long) sessionCode.getAttribute("time"); logger.info(early); long nowTime=System.currentTimeMillis(); logger.info(nowTime); if(nowTime-early<30000){ if(sessionCode.getAttribute("code").equals(codeInput)){ logger.info(codeInput); } }else{ sessionCode.removeAttribute("code"); sessionCode.removeAttribute("time"); logger.info("code已过期,请重新获取!"); } }else{ logger.info("请重新获取!"); } return null; }
相关文章推荐
- Cocoapod使用
- 配置vim
- struts2 modeldriven机制
- C primer plus 第八章 练习1:
- asp.net mvc4 webapi request获取参数
- 第七周 项目1成员函数、友元函数和一般函数有区别
- Paths
- Service和Thread的关系
- 十六进制数转十进制数
- ASP.NET MVC学习系列(二)-WebAPI请求
- Java基础回顾 : 异常的相关知识
- windows 系统 C语言写PHP简单扩展
- 并行计算的前世今生
- Java获取当前Ip
- java类变量与实例变量
- 定制班第6课
- 怎样改变Eclipse的左边目录显示结构
- 缓存依赖
- 未来的平台应沿着什么样的走向发展?
- [原创]java WEB学习笔记16:JSP指令(page,include),JSP标签(forwar,include,param)