日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 智能硬件 > 人工智能AI
[導(dǎo)讀] Kaptcha是一個(gè)基于SimpleCaptcha的驗(yàn)證碼開源項(xiàng)目。 kaptcha的使用比較方便,只需添加jar包依賴之后簡(jiǎn)單地配置就可以使用了。kaptcha所有配置都可以通過web

Kaptcha是一個(gè)基于SimpleCaptcha的驗(yàn)證碼開源項(xiàng)目。

kaptcha的使用比較方便,只需添加jar包依賴之后簡(jiǎn)單地配置就可以使用了。kaptcha所有配置都可以通過web.xml來完成,如果你的項(xiàng)目中使用了Spring MVC,那么則有另外的一種方式來實(shí)現(xiàn)。

一、簡(jiǎn)單的jsp-servlet項(xiàng)目 1.添加jar包依賴

如果你使用maven來統(tǒng)一管理jar包,則在工程的pom.xml中添加dependency

Xml代碼

《!-- kaptcha --》

《dependency》

《groupId》com.google.code.kaptcha《/groupId》

《arTIfacTId》kaptcha《/arTIfacTId》

《version》2.3.2《/version》

《/dependency》

《!-- kaptcha --》

《dependency》

《groupId》com.google.code.kaptcha《/groupId》

《artifactId》kaptcha《/artifactId》

《version》2.3.2《/version》

《/dependency》

如果是非maven管理的項(xiàng)目,則直接在官網(wǎng)下載kaptcha的jar包,然后添加到項(xiàng)目lib庫(kù)中。

2.配置web.xml

上面說了,kaptcha都是在web.xml中配置,我們必須在web.xml中配置kaptcha的servlet,具體如下:

Xml代碼

《servlet》

《servlet-name》Kaptcha《/servlet-name》

《servlet-class》com.google.code.kaptcha.servlet.KaptchaServlet《/servlet-class》

《/servlet》

《servlet-mapping》

《servlet-name》Kaptcha《/servlet-name》

《url-pattern》/kaptcha.jpg《/url-pattern》

《/servlet-mapping》

《servlet》

《servlet-name》Kaptcha《/servlet-name》

《servlet-class》com.google.code.kaptcha.servlet.KaptchaServlet《/servlet-class》

《/servlet》

《servlet-mapping》

《servlet-name》Kaptcha《/servlet-name》

《url-pattern》/kaptcha.jpg《/url-pattern》

《/servlet-mapping》

其中servlet的url-pattern可以自定義。

kaptcha所有的參數(shù)都有默認(rèn)的配置,如果我們不顯示配置的話,會(huì)采取默認(rèn)的配置。

如果要顯示配置kaptcha,在配置kaptcha對(duì)應(yīng)的Servlet時(shí),在init-param增加響應(yīng)的參數(shù)配置即可。示例如下:

Xml代碼

《servlet》

《servlet-name》Kaptcha《/servlet-name》

《servlet-class》com.google.code.kaptcha.servlet.KaptchaServlet《/servlet-class》

《init-param》

《param-name》kaptcha.image.width《/param-name》

《param-value》200《/param-value》

《description》Width in pixels of the kaptcha image.《/description》

《/init-param》

《init-param》

《param-name》kaptcha.image.height《/param-name》

《param-value》50《/param-value》

《description》Height in pixels of the kaptcha image.《/description》

《/init-param》

《init-param》

《param-name》kaptcha.textproducer.char.length《/param-name》

《param-value》4《/param-value》

《description》The number of characters to display.《/description》

《/init-param》

《init-param》

《param-name》kaptcha.noise.impl《/param-name》

《param-value》com.google.code.kaptcha.impl.NoNoise《/param-value》

《description》The noise producer.《/description》

《/init-param》

《/servlet》

《servlet》

《servlet-name》Kaptcha《/servlet-name》

《servlet-class》com.google.code.kaptcha.servlet.KaptchaServlet《/servlet-class》

《init-param》

《param-name》kaptcha.image.width《/param-name》

《param-value》200《/param-value》

《description》Width in pixels of the kaptcha image.《/description》

《/init-param》

《init-param》

《param-name》kaptcha.image.height《/param-name》

《param-value》50《/param-value》

《description》Height in pixels of the kaptcha image.《/description》

《/init-param》

《init-param》

《param-name》kaptcha.textproducer.char.length《/param-name》

《param-value》4《/param-value》

《description》The number of characters to display.《/description》

《/init-param》

《init-param》

《param-name》kaptcha.noise.impl《/param-name》

《param-value》com.google.code.kaptcha.impl.NoNoise《/param-value》

《description》The noise producer.《/description》

《/init-param》

《/servlet》

3.頁(yè)面調(diào)用

Html代碼

《formaction=“submit.action”》

《inputtype=“text”name=“kaptcha”value=“”/》《imgsrc=“kaptcha.jpg”/》

《/form》

《form action=“submit.action”》

《input type=“text” name=“kaptcha” value=“” /》《img src=“kaptcha.jpg” /》

《/form》

4.在submit的action方法中進(jìn)行驗(yàn)證碼校驗(yàn)

Java代碼

//從session中取出servlet生成的驗(yàn)證碼text值

String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

//獲取用戶頁(yè)面輸入的驗(yàn)證碼

String kaptchaReceived = request.getParameter(“kaptcha”);

//校驗(yàn)驗(yàn)證碼是否正確

if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)){

setError(“kaptcha”, “Invalid validation code.”);

}

//從session中取出servlet生成的驗(yàn)證碼text值

String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

//獲取用戶頁(yè)面輸入的驗(yàn)證碼

String kaptchaReceived = request.getParameter(“kaptcha”);

//校驗(yàn)驗(yàn)證碼是否正確

if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)){

setError(“kaptcha”, “Invalid validation code.”);

}

注:確保JDK設(shè)置了 -Djava.awt.headless=true

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉