使用重寫url機(jī)制實(shí)現(xiàn)驗(yàn)證碼換一張功能
重寫URL機(jī)制:為了保證一個(gè)url的地址唯一,可每次向服務(wù)器傳遞的參數(shù)不一樣即可。
由數(shù)據(jù)請(qǐng)求的抱頭信息可分析到:抱頭信息包括http協(xié)議,IP地址,端口號(hào),工程名,請(qǐng)求參數(shù)列表,要想訪問(wèn)的資源不發(fā)生變化,只能變化參數(shù)連表。
此處在實(shí)現(xiàn)驗(yàn)證碼的換一張的功能時(shí),就是利用了改變參數(shù)列表的值進(jìn)行刷新。
詳細(xì)代碼實(shí)現(xiàn):
<%@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();
%>
添加登錄頁(yè)面:
<%@ 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();//生成時(shí)間戳,
document.getElementById("img").src="image.jsp?t="+d;//由變化的時(shí)間使參數(shù)連表發(fā)生變化,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>
利用時(shí)間的變化,每次生成時(shí)間戳,傳參給請(qǐng)求的url,達(dá)到重寫url的目的,從而實(shí)現(xiàn)了換一張的刷新功能。

總結(jié)
以上所述是小編給大家介紹的使用重寫url機(jī)制實(shí)現(xiàn)驗(yàn)證碼換一張功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
layui 上傳文件_批量導(dǎo)入數(shù)據(jù)UI的方法
今天小編就為大家分享一篇layui 上傳文件_批量導(dǎo)入數(shù)據(jù)UI的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
TypeScript?背后的結(jié)構(gòu)化類型系統(tǒng)原理詳解
這篇文章主要為大家介紹了TypeScript?背后的結(jié)構(gòu)化類型系統(tǒng)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Bootstrap簡(jiǎn)單實(shí)用的表單驗(yàn)證插件BootstrapValidator用法實(shí)例詳解
這篇文章主要介紹了Bootstrap簡(jiǎn)單實(shí)用的表單驗(yàn)證插件BootstrapValidator用法,結(jié)合實(shí)例形式詳細(xì)分析了Bootstrap表單驗(yàn)證插件BootstrapValidator基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
js中一個(gè)函數(shù)獲取另一個(gè)函數(shù)返回值問(wèn)題探討
在本文將為大家詳細(xì)探討下js中一個(gè)函數(shù)獲取另一個(gè)函數(shù)返回值問(wèn)題,比較模糊的朋友可以學(xué)習(xí)下哦2013-11-11
一個(gè)網(wǎng)頁(yè)標(biāo)題title的閃動(dòng)提示效果實(shí)現(xiàn)思路
通過(guò)網(wǎng)頁(yè)title來(lái)提示用戶有新消息這個(gè)功能很常見(jiàn),下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-03-03
JavaScript實(shí)現(xiàn)創(chuàng)建自定義對(duì)象的常用方式總結(jié)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)創(chuàng)建自定義對(duì)象的常用方式,結(jié)合實(shí)例形式總結(jié)分析了JavaScript工廠模式、構(gòu)造函數(shù)模式、原型模式、組合模式等常用的自定義對(duì)象創(chuàng)建模式操作與使用技巧,需要的朋友可以參考下2018-07-07
JavaScript setTimeout和setInterval的使用方法 說(shuō)明
兩個(gè)函數(shù)都是可以用來(lái)實(shí)現(xiàn)一段時(shí)間后執(zhí)行一段javascript代碼的效果。兩個(gè)函數(shù)都有兩個(gè)參數(shù),前面的都是執(zhí)行表達(dá)式,后面的是隔的秒數(shù)。2010-03-03

