Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
頭條創(chuàng)作挑戰(zhàn)賽# 在Excel VBA中,HTMLMetaElement對象是表示HTML文檔中的元數(shù)據(jù)的一種對象。元數(shù)據(jù)是描述文檔內(nèi)容的信息,例如作者、關(guān)鍵字、描述等。HTMLMetaElement對象可以讓您從HTML文檔中獲取元數(shù)據(jù),并在VBA代碼中使用它們。您可以使用HTMLDocument對象來訪問HTML文檔中的元素,包括HTMLMetaElement對象。
以下是6個(gè)Excel VBA代碼示例,演示如何使用HTMLMetaElement對象:
1、獲取文檔中的所有元數(shù)據(jù)
Sub GetMetadata()
Dim metaTags As Object
Dim metaTag As Object
Set metaTags=ActiveSheet.QueryTables(1).ResultPageRange.getElementsByTagName("meta")
For Each metaTag In metaTags
Debug.Print metaTag.Name & ": " & metaTag.Content
Next metaTag
End Sub
2、獲取特定元數(shù)據(jù)
Sub GetSpecificMetadata()
Dim metaTag As Object
Set metaTag=ActiveSheet.QueryTables(1).ResultPageRange.querySelector("meta[name='description']")
Debug.Print "Description: " & metaTag.Content
End Sub
3、獲取文檔標(biāo)題
Sub GetTitle()
Dim title As Object
Set title=ActiveSheet.QueryTables(1).ResultPageRange.querySelector("title")
Debug.Print "Title: " & title.innerText
End Sub
4、獲取關(guān)鍵字
Sub GetKeywords()
Dim metaTag As Object
Set metaTag=ActiveSheet.QueryTables(1).ResultPageRange.querySelector("meta[name='keywords']")
Debug.Print "Keywords: " & metaTag.Content
End Sub
5、獲取作者
Sub GetAuthor()
Dim metaTag As Object
Set metaTag=ActiveSheet.QueryTables(1).ResultPageRange.querySelector("meta[name='author']")
Debug.Print "Author: " & metaTag.Content
End Sub
6、獲取文檔語言
vbnet復(fù)制Sub GetLanguage()
Dim html As Object
Set html=ActiveSheet.QueryTables(1).ResultPageRange.querySelector("html")
Debug.Print "Language: " & html.getAttribute("lang")
End Sub
以下是2個(gè)完整的Excel VBA代碼案例,演示如何使用HTMLMetaElement對象:
1、獲取特定網(wǎng)站的元數(shù)據(jù)并將其寫入Excel表格
Sub GetWebsiteMetadata()
Dim xmlHttp As Object
Dim htmlDoc As Object
Dim metaTags As Object
Dim metaTag As Object
Dim i As Integer
Set xmlHttp=CreateObject("MSXML2.XMLHTTP")
xmlHttp.Open "GET", "https://www.example.com", False
xmlHttp.send
Set htmlDoc=CreateObject("htmlfile")
htmlDoc.body.innerHTML=xmlHttp.responseText
Set metaTags=htmlDoc.getElementsByTagName("meta")
For Each metaTag In metaTags
If metaTag.Name="description" Then
Cells(1, 1)="Description"
Cells(1, 2)=metaTag.Content
ElseIf metaTag.Name="keywords" Then
Cells(2, 1)="Keywords"
Cells(2, 2)=metaTag.Content
End If
Next metaTag
Set xmlHttp=Nothing
Set htmlDoc=Nothing
End Sub
2、獲取當(dāng)前工作簿中的元數(shù)據(jù)并將其寫入Excel表格
Sub GetWorkbookMetadata()
Dim wb As Workbook
Dim ws As Worksheet
Dim metaTags As Object
Dim metaTag As Object
Dim i As Integer
Set wb=ThisWorkbook
Set ws=wb.Sheets("Sheet1")
Set metaTags=ws.QueryTables(1).ResultPageRange.getElementsByTagName("meta")
For Each metaTag In metaTags
If metaTag.Name="description" Then
Cells(1, 1)="Description"
Cells(1, 2)=metaTag.Content
ElseIf metaTag.Name="keywords" Then
Cells(2, 1)="Keywords"
Cells(2, 2)=metaTag.Content
End If
Next metaTag
End Sub
在使用VBA中的HTMLMetaElement時(shí),需要注意以下幾點(diǎn):
1、需要先加載HTML文檔,才能訪問其中的元素。可以使用XMLHttpRequest對象或者InternetExplorer對象來加載HTML文檔。
2、元數(shù)據(jù)的名稱和值都可以使用Content屬性訪問。例如,獲取文檔的描述信息可以使用“metaTag.Content”。
3、如果元數(shù)據(jù)的名稱是動態(tài)生成的,則可以使用querySelector方法來查找元素。例如,獲取名稱為“description”的元數(shù)據(jù)可以使用“querySelector("meta[name='description']")”。
4、如果元數(shù)據(jù)的名稱是固定的,則可以直接使用getElementsByTagName方法來獲取所有的meta元素,并遍歷其中的元素來查找需要的元素。
5、在訪問元數(shù)據(jù)之前,需要確保文檔已經(jīng)完全加載。可以使用ReadyState屬性來檢查文檔的加載狀態(tài)。
6、在訪問元數(shù)據(jù)時(shí),需要考慮元數(shù)據(jù)是否存在。如果元數(shù)據(jù)不存在,則訪問Content屬性會導(dǎo)致運(yùn)行時(shí)錯(cuò)誤。因此,建議在訪問元數(shù)據(jù)之前先檢查元素是否存在。
7、在訪問元數(shù)據(jù)時(shí),需要考慮元數(shù)據(jù)的編碼方式。如果元數(shù)據(jù)中包含特殊字符或非ASCII字符,則需要使用適當(dāng)?shù)木幋a方式進(jìn)行解碼。可以使用VBA內(nèi)置的StrConv函數(shù)來進(jìn)行編碼轉(zhuǎn)換。
、認(rèn)識DOM對象模型
DOM:Document Object Model(文檔對象模型)
節(jié)點(diǎn)與節(jié)點(diǎn)的關(guān)系
1、訪問節(jié)點(diǎn),使用getElement系列方法訪問指定節(jié)點(diǎn)
getElementById()、 getElementsByName()、
getElementsByTagName();
2、根據(jù)層次關(guān)系訪問節(jié)點(diǎn):
屬性名稱 描述
parentNode 返回節(jié)點(diǎn)的父節(jié)點(diǎn)
childNodes 返回子節(jié)點(diǎn)集合,childNodes[i]
firstChild 返回節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn),最普遍的用法是訪問該元素的文本節(jié)點(diǎn)
lastChild 返回節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)
nextSibling 下一個(gè)節(jié)點(diǎn)
previousSibling 上一個(gè)節(jié)點(diǎn)
elment屬性:
屬性名稱 描述
firstElementChild 返回節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn),最普遍的用法是訪問該元素的文本節(jié)點(diǎn)
lastElementChild 返回節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)
nextElementSibling 下一個(gè)節(jié)點(diǎn)
previousElementSibling 上一個(gè)節(jié)點(diǎn)
節(jié)點(diǎn)信息表示:
nodeName:節(jié)點(diǎn)名稱
nodeValue:節(jié)點(diǎn)值
nodeType:節(jié)點(diǎn)類型
操作節(jié)點(diǎn)的屬性:
getAttribute("屬性名")
setAttribute("屬性名","屬性值")
創(chuàng)建和插入節(jié)點(diǎn):
名稱 描述
createElement( tagName)創(chuàng)建一個(gè)標(biāo)簽名為tagName的新元素節(jié)點(diǎn)
A.appendChild( B)把B節(jié)點(diǎn)追加至A節(jié)點(diǎn)的末尾
insertBefore( A,B )把A節(jié)點(diǎn)插入到B節(jié)點(diǎn)之前
cloneNode(boolean)復(fù)制(克隆)某個(gè)指定的節(jié)點(diǎn)
刪除和替換節(jié)點(diǎn):
名稱描述
removeChild( node)刪除指定的節(jié)點(diǎn)
replaceChild( newNode, oldNode)屬性attr 用其他的節(jié)點(diǎn)替換指定的節(jié)點(diǎn)
操作節(jié)點(diǎn)樣式:
改變樣式的屬性
style屬性
HTML元素.style.樣式屬性="值";
className屬性
HTML元素.className="樣式名稱";
二、示例展示
1.輪播圖示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>輪播圖顯示</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
#outer {
width: 300px;
margin: 50px auto;
padding: 10px;
background-color: greenyellow;
text-align: center;
}
#outer>img {
width: 300px;
height: 300px;
}
</style>
<script>
window.onload=function () {
// 點(diǎn)擊按鈕切換圖片
var prev=document.getElementById("prev");
var next=document.getElementById("next");
// 要切換圖片就要修改img標(biāo)簽的src屬性
var img=document.getElementsByTagName("img")[0];
var imgArr=["img/tou01.jpg", "img/tou02.jpg", "img/tou03.jpg", "img/tou04.jpg"];
// 創(chuàng)建一個(gè)變量,來保存當(dāng)前正在顯示的圖片的索引
var index=0;
// 設(shè)置提示文字
var info=document.getElementById("info");
// 分別為兩個(gè)按鈕綁定單機(jī)響應(yīng)函數(shù) 上一張函數(shù)
prev.onclick=function () {
index--;
// 判斷index是否小于0
if (index < 0) {
index=imgArr.length-1;
}
img.src=imgArr[index];
info.innerHTML="共"+imgArr.length+"張圖片"+"當(dāng)前是第"+(index+1)+"張";
}
// 下一張函數(shù)
next.onclick=function () {
index++;
if (index > imgArr.length - 1) {
index=0;
}
img.src=imgArr[index];
// 當(dāng)我在點(diǎn)擊按鈕以后在重新執(zhí)行一遍
info.innerHTML="一共"+imgArr.length+"張圖片"+"當(dāng)前是第"+(index+1)+"張";
}
}
</script>
<body>
<div id="outer">
<p id="info">一共4張圖片當(dāng)前是第1張</p>
<img src="img/tou01.jpg" alt="" />
<button id="prev"><上一張</button>
<button id="next">下一張></button>
</div>
</body>
</html>
2.論壇發(fā)帖示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>制作課工場論壇發(fā)貼</title>
</head>
<style>
*{margin: 0; padding: 0; font-family: "Arial", "微軟雅黑";}
ul,li{list-style: none;}
.bbs{margin: 0 auto; width: 600px; position: relative;}
header{padding: 5px 0; border-bottom: 1px solid #cecece;}
header span{display:inline-block; width: 220px; height: 50px; color: #fff; background: #009966; font-size: 18px; font-weight: bold; text-align: center;line-height: 50px; border-radius: 8px; cursor: pointer;}
.post{position: absolute; background: #ffffff; border: 1px #cccccc solid; width: 500px; left: 65px; top:70px; padding: 10px; font-size: 14px; z-index: 999999; display: none;}
.post .title{width: 450px; height:30px; line-height: 30px; display: block; border: 1px #cecece solid; margin-bottom: 10px;}
.post select{width: 200px; height: 30px;}
.post .content{width: 450px; height: 200px; display: block; margin: 10px 0;border: 1px #cecece solid;}
.post .btn{width: 160px; height: 35px; color: #fff; background: #009966; border: none; font-size: 14px; font-weight: bold; text-align: center; line-height: 35px; border-radius: 8px; cursor: pointer;}
.bbs section ul li{padding: 10px 0; border-bottom: 1px #999999 dashed;
overflow: hidden;}
.bbs section ul li div{float: left; width: 60px; margin-right: 10px;}
.bbs section ul li div img{ border-radius:50%; width: 60px;}
.bbs section ul li h1{float: left; width: 520px; font-size: 16px; line-height: 35px;}
.bbs section ul li p{color: #666666; line-height: 25px; font-size: 12px; }
.bbs section ul li p span{padding-right:20px;}
</style>
<body>
<div class="bbs">
<header><span onclick="showDiv();">我要發(fā)帖</span></header>
<section>
<ul id="showContent"></ul>
</section>
<div class="post" id="showSubmit">
<input class="title" placeholder="請輸入標(biāo)題(1-50個(gè)字符)" id="title">所屬版塊:
<select id="section">
<option>請選擇版塊</option>
<option value="電子書籍">電子書籍</option>
<option value="新課來了">新課來了</option>
<option value="新手報(bào)到">新手報(bào)到</option>
<option value="職業(yè)規(guī)劃">職業(yè)規(guī)劃</option>
</select>
<textarea class="content" id="content"></textarea>
<input class="btn" value="發(fā)布" onclick="publish();">
</div>
</div>
</body>
<script src="js/bbs.js"></script>
</html>
// 全局對象
var imgs=new Array("tou01.jpg", "tou02.jpg", "tou03.jpg", "tou04.jpg");
// 顯示發(fā)帖div
function showDiv() {
document.getElementById("showSubmit").style.display="block";
}
// 點(diǎn)擊發(fā)布添加內(nèi)容到Li
function publish() {
// 獲得隨機(jī)頭像的數(shù)組下標(biāo)
var index=Math.floor(Math.random() * 4);
// 創(chuàng)建li節(jié)點(diǎn)
var tvLi=document.createElement("li");
// 創(chuàng)建div節(jié)點(diǎn)
var tvDiv=document.createElement("div");
// 創(chuàng)建img圖片節(jié)點(diǎn)
var tvImg=document.createElement("img");
// 設(shè)置圖片節(jié)點(diǎn)src屬性
tvImg.setAttribute("src", "../threeClass/img/" + imgs[index]);
// div添加圖片為子節(jié)點(diǎn)
tvDiv.appendChild(tvImg);
// 創(chuàng)建h1標(biāo)簽節(jié)點(diǎn)
var tvh1=document.createElement("h1");
// 取得發(fā)布div框里填充的標(biāo)題的值填充到h1標(biāo)簽
var tvTitle=document.getElementById("title").value;
tvh1.innerText=tvTitle;
// 創(chuàng)建一個(gè)P標(biāo)簽節(jié)點(diǎn)
var tvP=document.createElement("p");
// 創(chuàng)建兩個(gè)span標(biāo)簽節(jié)點(diǎn)
var tvSpanOne=document.createElement("span");
var tvSpanTwo=document.createElement("span");
// 第一個(gè)span標(biāo)簽取填充div里的下拉列表框所選的值
var tvSelect=document.getElementById("section").value;
tvSpanOne.innerText="板塊:" + tvSelect;
// 第二個(gè)span標(biāo)簽取當(dāng)前系統(tǒng)時(shí)間
var date=new Date();
var str=date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + "" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
tvSpanTwo.innerText="時(shí)間是:" + str;
// 兩個(gè)span標(biāo)簽追加到p標(biāo)簽節(jié)點(diǎn)里
tvP.appendChild(tvSpanOne);
tvP.appendChild(tvSpanTwo);
// 把div、h1、p 、標(biāo)簽追加到li里
tvLi.appendChild(tvDiv);
tvLi.appendChild(tvh1);
tvLi.appendChild(tvP);
// 把添加好的li在插入到ul標(biāo)簽節(jié)點(diǎn)里
var oldUL=document.getElementById("showContent");
// 把新添加的li節(jié)點(diǎn)插入到撈的li節(jié)點(diǎn)之前
oldUL.insertBefore(tvLi, oldUL.firstChild);
// 清除div里填充過的內(nèi)容,如標(biāo)題和內(nèi)容部分
document.getElementById("title").value="";
document.getElementById("content").value="";
// 設(shè)置發(fā)布div隱藏
document.getElementById("showSubmit").style.display="none";
}
效果圖展示:file:///D:/ruanjian/VS/JS/threeClass/lunbotu.html
file:///D:/ruanjian/VS/JS/threeClass/lunbotu.html
網(wǎng)頁頁面的使用中為防止“非人類”的大量操作和防止一些的信息冗余,增加驗(yàn)證碼校驗(yàn)是許多網(wǎng)站常用的方式。
而讓用戶輸入字母和數(shù)字組合的驗(yàn)證碼是最經(jīng)典也是最常用的方式。
這一篇是純利用現(xiàn)有JDK提供的繪圖類(ImageIO)類制作,這個(gè)過程比較復(fù)雜且需要了解ImageIO類。
今天發(fā)布的第二篇文章是利用Hutool工具類來實(shí)現(xiàn)的,該工具類已經(jīng)封裝驗(yàn)證碼所需的相關(guān)類等,使用起來較為簡單和方便。
驗(yàn)證碼的生成和校驗(yàn)過程均使用Servlet和JSP的結(jié)合來實(shí)現(xiàn),Servlet的相關(guān)內(nèi)容可以參閱
Servlet技術(shù):https://mp.weixin.qq.com/s/__e_ef0SI6kVPiRaU0MXJw
如何利用基礎(chǔ)的JSP知識來實(shí)現(xiàn)網(wǎng)頁的驗(yàn)證碼校驗(yàn)?zāi)兀?/span>
首先要驗(yàn)證碼的校驗(yàn)的過程。
驗(yàn)證碼校驗(yàn)分為三部分:
驗(yàn)證碼的生成實(shí)際就是輸出一個(gè)圖像,所以在這里使用ImageIO來生成圖片,然后結(jié)合使用隨機(jī)數(shù)(Random)來實(shí)現(xiàn)隨機(jī)生成驗(yàn)證上的內(nèi)容,最后進(jìn)而展示出來,然后利用Session對象存儲驗(yàn)證碼的內(nèi)容。在用戶輸入驗(yàn)證碼的時(shí)候可以用request來獲取用戶輸入的內(nèi)容,讓其余Session對象中保存的驗(yàn)證碼內(nèi)容進(jìn)行比較,若一致則驗(yàn)證成功,不一致就驗(yàn)證失敗。
先創(chuàng)建一個(gè)圖片的緩沖區(qū):
BufferedImage bi=new BufferedImage(68, 22,BufferedImage.TYPE_INT_RGB);
創(chuàng)建畫布:
Graphics g=bi.getGraphics();
創(chuàng)建顏色:
Color c=new Color(200,150,255);
創(chuàng)建背景顏色:
g.setColor(c);
填充矩形:
g.fillRect(0, 0, 68,22);
將要顯示的驗(yàn)證碼內(nèi)容組成元素存入字符串?dāng)?shù)組:
char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
創(chuàng)建隨機(jī)的驗(yàn)證碼內(nèi)容:
Random r=new Random();
int len=ch.length;
int index; //index用于存放隨機(jī)數(shù)字
StringBuffer sb=new StringBuffer();
for(int i=0;i<4;i++)
{
index=r.nextInt(len);//產(chǎn)生隨機(jī)數(shù)字
g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255))); //設(shè)置顏色
g.drawString(ch[index]+"",(i*15)+3, 18);//畫數(shù)字以及數(shù)字的位置
sb.append(ch[index]);
}
將驗(yàn)證碼的內(nèi)容存入Session及顯示在頁面上:
request.getSession().setAttribute("piccode",sb.toString());
ImageIO.write(bi, "JPG", response.getOutputStream());
完整代碼:
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedImage bi=new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);//創(chuàng)建圖像緩沖區(qū)
Graphics g=bi.getGraphics(); //通過緩沖區(qū)創(chuàng)建一個(gè)畫布
Color c=new Color(200, 150, 255); //創(chuàng)建顏色
g.setColor(c);//為畫布創(chuàng)建背景顏色
g.fillRect(0, 0, 68, 22); //填充矩形
char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();//轉(zhuǎn)化為字符型的數(shù)組
Random r=new Random();
int len=ch.length;
int index; //index用于存放隨機(jī)數(shù)字
StringBuffer sb=new StringBuffer();
for (int i=0; i < 4; i++) {
index=r.nextInt(len);//產(chǎn)生隨機(jī)數(shù)字
g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt(255))); //設(shè)置顏色
g.drawString(ch[index] + "", (i * 15) + 3, 18);//畫數(shù)字以及數(shù)字的位置
sb.append(ch[index]);
}
request.getSession().setAttribute("piccode", sb.toString());
ImageIO.write(bi, "JPG", response.getOutputStream());
}
}
在測試之前需要先在web.xml文件中配置一下:
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.kailong.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/imageServlet</url-pattern>
</servlet-mapping>
啟動服務(wù)器后在瀏覽器中輸入http://localhost:8080/工程名/imageServlet 即可
驗(yàn)證碼的生成已經(jīng)實(shí)現(xiàn)成功,下面實(shí)現(xiàn)驗(yàn)證驗(yàn)證碼的Servlet。
先新建一個(gè)jsp用戶界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄界面</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/loginServlet" method="get" >
驗(yàn)證碼:<input type="text" name="checkCode"/><br/>
<img alt="驗(yàn)證碼" id="imagecode" src="<%=request.getContextPath()%>/imageServlet"/>
<input type="submit" value="提交">
</form>
</body>
</html>
校驗(yàn)驗(yàn)證碼過程:
代碼實(shí)現(xiàn):
獲取Session中的驗(yàn)證碼內(nèi)容:
String piccode=(String) request.getSession().getAttribute("piccode");
獲取用戶輸入的驗(yàn)證碼內(nèi)容:
String checkCode=request.getParameter("checkCode");
驗(yàn)證碼判斷(使用了PrintWriter將相關(guān)內(nèi)容輸出)
response.setContentType("text/html;charset=utf-8");//解決亂碼問題
PrintWriter out=response.getWriter();
if(checkCode.equals(piccode))
{
out.println("驗(yàn)證碼輸入正確!");
}
else
{
out.println("驗(yàn)證碼輸入錯(cuò)誤!!!");
}
out.flush();//將流刷新
out.close();//將流關(guān)閉
完整代碼:
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
//用于驗(yàn)證驗(yàn)證碼
{
String piccode=(String) request.getSession().getAttribute("piccode");
String checkCode=request.getParameter("checkCode");
response.setContentType("text/html;charset=utf-8");//解決亂碼問題
PrintWriter out=response.getWriter();
if (checkCode.equals(piccode)) {
out.println("驗(yàn)證碼輸入正確!");
} else {
out.println("驗(yàn)證碼輸入錯(cuò)誤!!!");
}
out.flush();//將流刷新
out.close();//將流關(guān)閉
}
}
測試前先在web.xml文件中配置一下:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.kailong.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
在驗(yàn)證碼生成之后,用戶在識別的時(shí)候可能不能正確識別,這時(shí)候就需要刷新一下重新生成。
添加超鏈接實(shí)現(xiàn)刷新:
login.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄界面</title>
<script>
function reloadCode() {
var time=new Date().getTime();
document.getElementById("imagecode").src="<%=request.getContextPath()%>/imageGenerate?d="+time;
}
</script>
</head>
<body>
<form action="<%=request.getContextPath()%>/loginServlet" method="get" >
驗(yàn)證碼:<input type="text" name="checkCode"/><br/>
<img alt="驗(yàn)證碼" id="imagecode" src="<%=request.getContextPath()%>/imageServlet"/>
<a href="javascript:reloadCode();">看不清楚</a><br>
<br/><input type="submit" value="提交">
</form>
</body>
</html>
js部分的Date相關(guān)是防止瀏覽器緩存后不能正常刷新,添加時(shí)間的唯一性來實(shí)現(xiàn)能夠及時(shí)刷新和展示。
js 部分可以參閱:JavaScript 語言入門: https://mp.weixin.qq.com/s/37CaC25_1agb-aXBLhUKtg
也可以在ImageServlet中添加防止瀏覽器緩存的語句:
response.setHeader("Pragma", "No-cache");
公眾號本文地址:https://mp.weixin.qq.com/s/XHucabQ_WwUx2OMDGSTMkw
歡迎關(guān)注公眾號:愚生淺末。
*請認(rèn)真填寫需求信息,我們會在24小時(shí)內(nèi)與您取得聯(lián)系。