利用java實(shí)現(xiàn)驗(yàn)證碼生成并完成驗(yàn)證?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司專(zhuān)注于策勒網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供策勒營(yíng)銷(xiāo)型網(wǎng)站建設(shè),策勒網(wǎng)站制作、策勒網(wǎng)頁(yè)設(shè)計(jì)、策勒網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造策勒網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供策勒網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
java 制作驗(yàn)證碼并進(jìn)行驗(yàn)證實(shí)例詳解
在注冊(cè)、登錄的頁(yè)面上經(jīng)常會(huì)出現(xiàn)驗(yàn)證碼,為了防止頻繁的注冊(cè)或登錄行為。下面是我用java制作的一個(gè)驗(yàn)證碼,供初學(xué)者參考,做完驗(yàn)證碼之后,我們可以用ajax進(jìn)行驗(yàn)證碼驗(yàn)證。
功能一:驗(yàn)證碼制作的代碼,點(diǎn)擊圖片,驗(yàn)證碼進(jìn)行更換
/**
* 顯示驗(yàn)證碼圖片
*/
public void showCheckCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 調(diào)用業(yè)務(wù)邏輯
String checkCode = getCheckCode();
//將驗(yàn)證碼字符放入session域?qū)ο笾?
req.getSession().setAttribute("checkCode", checkCode);
//圖片寬
int width = 80;
//圖片高
int height = 30;
//在內(nèi)存中創(chuàng)建一個(gè)圖片
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取一個(gè)畫(huà)筆
Graphics g = image.getGraphics();
//設(shè)置畫(huà)筆顏色,用灰色做背景
g.setColor(Color.GRAY);
//向Image中填充灰色
g.fillRect(0,0,width,height);
Random r = new Random();
//設(shè)置3條干擾線
for (int i = 0; i < 3; i++) {
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.drawLine(r.nextInt(80), r.nextInt(30), r.nextInt(80), r.nextInt(80));
}
//設(shè)置驗(yàn)證碼字符串的顏色
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
//設(shè)置字符的大小
g.setFont(new Font("黑體",Font.BOLD,24));
//在圖片中寫(xiě)入驗(yàn)證碼字符串
g.drawString(checkCode,15,20);
//將Image對(duì)象以PNG格式輸出給所有的客戶端
ImageIO.write(image,"PNG",resp.getOutputStream());
}
/**
* 獲取4位驗(yàn)證碼中的4位隨機(jī)字符串
*/
public static String getCheckCode(){
//驗(yàn)證碼中的字符由數(shù)字和大小寫(xiě)字母組成
String code = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
Random r = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
sb.append(code.charAt(r.nextInt(code.length())));
}
return sb.toString();
}jsp頁(yè)面
<script type="text/javascript">
function changeCodeImage(img){
img.src = "${pageContext.request.contextPath}/UserServlet?method=showCheckCode&time="+new Date().getTime();
}
</script>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">驗(yàn)證碼</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="writeCode" onkeyup="checkCodeMethod(this.value)" >
</div>
<div class="col-sm-2">
<img src="${pageContext.request.contextPath}/UserServlet?method=showCheckCode" id="checkCodeImage" title="點(diǎn)擊換一張" onclick="changeCodeImage(this)" />
</div>
<span id="checkCodeSpan"></span>
</div>功能二:ajax動(dòng)態(tài)驗(yàn)證驗(yàn)證碼
/**
* 驗(yàn)證驗(yàn)證碼
*/
public void checkCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取從頁(yè)面中接收到的驗(yàn)證碼參數(shù)
String checkCode = req.getParameter("checkCode");
//從session域?qū)ο笾蝎@取驗(yàn)證碼
String sessionCode = (String) req.getSession().getAttribute("checkCode");
//判斷驗(yàn)證碼是否相同
if (checkCode.equalsIgnoreCase(sessionCode)) {
resp.getWriter().print(true);
}else {
resp.getWriter().print(false);
}jsp頁(yè)面
<script type="text/javascript">
function changeCodeImage(img){
img.src = "${pageContext.request.contextPath}/UserServlet?method=showCheckCode&time="+new Date().getTime();
}
function checkCodeMethod(code){
$.get("${pageContext.request.contextPath}/UserServlet?method=checkCode",
{ checkCode: code},
function(data){
if (data == 'true') {
document.getElementById("checkCodeSpan").innerHTML = "<font>驗(yàn)證碼正確!</font>";
}else {
document.getElementById("checkCodeSpan").innerHTML = "<font>驗(yàn)證碼錯(cuò)誤!</font>";
}
}
);
}
</script>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">驗(yàn)證碼</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="writeCode" onkeyup="checkCodeMethod(this.value)" >
</div>
<div class="col-sm-2">
<img src="${pageContext.request.contextPath}/UserServlet?method=showCheckCode" id="checkCodeImage" title="點(diǎn)擊換一張" onclick="changeCodeImage(this)" />
</div>
<span id="checkCodeSpan"></span>
</div>看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
標(biāo)題名稱(chēng):利用java實(shí)現(xiàn)驗(yàn)證碼生成并完成驗(yàn)證
轉(zhuǎn)載來(lái)源:http://www.yijiale78.com/article34/gipjpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、App設(shè)計(jì)、微信公眾號(hào)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)