微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用重写url机制实现验证码换一张功能

重写URL机制:为了保证一个url的地址唯一,可每次向服务器传递的参数不一样即可。

由数据请求的抱头信息可分析到:抱头信息包括http协议,IP地址,端口号,工程名,请求参数列表,要想访问的资源不发生变化,只能变化参数连表。

此处在实现验证码的换一张的功能时,就是利用了改变参数列表的值进行刷新。

详细代码实现:

rush:js;"> <%@page import="javax.imageio.ImageIO"%> <%@page import="java.awt.Font"%> <%@page import="java.awt.Color"%> <%@page import="java.awt.Graphics"%> <%@page import="java.awt.image.BufferedImage"%> <%@ page contentType="image/jpeg" language="java" import="java.util.*" pageEncoding="UTF-8"%> <% int w=100; int h=30; BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics g=bi.getGraphics(); Color c=g.getColor(); Font f=g.getFont(); Random r=new Random(); Color bg=new Color(150+r.nextInt(100),150+r.nextInt(100),150+r.nextInt(100)); g.setColor(bg); g.fillRect(0,w,h); String code=""; for(int i=1;i<=4;i++){ int num=r.nextInt(10); code=code+num; Color num_c=new Color(r.nextInt(150),r.nextInt(150),r.nextInt(150)); g.setColor(num_c); g.drawString(String.valueOf(num),20*i,h/2); } request.getSession().setAttribute("code",code); //清空缓存 response.setHeader("pragma","bo-cache"); response.setHeader("cache-control","bo-cache"); response.addDateHeader("expires",0); ImageIO.write(bi,"jpeg",response.getOutputStream()); out.close(); %>

添加登录页面

rush:js;"> <%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" pageEncoding="UTF-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> nofollow" > My JSP 'login.jsp' starting page <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"> cope.msg }
pwd: code:

利用时间的变化,每次生成时间戳,传参给请求的url,达到重写url的目的,从而实现了换一张的刷新功能

总结

以上所述是小编给大家介绍的使用重写url机制实现验证码换一张功能。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐