使用重寫url機制實現驗證碼換一張功能,url驗證碼

來源:互聯網
上載者:User

使用重寫url機制實現驗證碼換一張功能,url驗證碼

 重寫URL機制:為了保證一個url的地址唯一,可每次向伺服器傳遞的參數不一樣即可。

由資料請求的抱頭資訊可分析到:抱頭資訊包括http協議,IP地址,連接埠號碼,工程名,請求參數列表,要想訪問的資源不發生變化,只能變化參數連表。

此處在實現驗證碼的換一張的功能時,就是利用了改變參數列表的值進行重新整理。

詳細代碼實現:

<%@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, 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();  %> 

添加登入頁面: 

<%@ 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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>  <head>   <base href="<%=basePath%>" rel="external nofollow" >   <title>My JSP 'login.jsp' starting page</title>   <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">   <!--   <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >   -->   <script type="text/javascript"> function changeimage(){ var d=new Date();//產生時間戳記, document.getElementById("img").src="image.jsp?t="+d;//由變化的時間使參數連表發生變化,url重寫 } </script>  </head>  <body>  <font color="red">${requestScope.msg }</font>   <form action="loginServlet" method="post">   name:<input type="text" name="uname"><br>   pwd:<input type="pwd" name="upwd"><br>   code:<input type="text" name="code" size="5"><img id="img" alt="" src="image.jsp "><a onclick="changeimage()">換一張</a><br>   <input type="submit" >   </form>  </body> </html> 

利用時間的變化,每次產生時間戳記,傳參給請求的url,達到重寫url的目的,從而實現了換一張的重新整理功能。

總結

以上所述是小編給大家介紹的使用重寫url機制實現驗證碼換一張功能,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.