javaWeb使用Kaptcha組件生成驗證碼
javaWeb之使用Kaptcha組件使用驗證碼
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>kaptcha</display-name> <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>/randomcode.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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%>">
<title>My JSP 'index.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">
-->
</head>
<body>
<form action="check.jsp">
<img alt="" src="randomcode.jpg">
<input type="text" name="r"/>
<input type="submit" />
</form>
</body>
</html>
check.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
//檢測是否是正確的驗證碼
String k=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String str=request.getParameter("r");
if(k.equals(str))
out.print("true");
out.print(" "+k+"---"+str);
%>
結(jié)果:

驗證碼部分屬性修改說明:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)組與堆棧相關(guān)知識總結(jié)
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著Java數(shù)組與堆棧展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
spring Boot查詢數(shù)據(jù)分頁顯示的方法實例
這篇文章主要給大家介紹了關(guān)于spring Boot查詢數(shù)據(jù)分頁顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-08-08
SpringBoot中@EnableAutoConfiguration注解源碼分析
這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration注解源碼分析,@EnableAutoConfiguration,主要是用于加載Starter目錄包之外的、需要Spring自動生成Bean對象的、帶有@Configuration注解的類,需要的朋友可以參考下2023-08-08
在Java中避免NullPointerException的解決方案
這篇文章主要介紹了在Java中避免NullPointerException的解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource
本文技術(shù)了???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource,下文詳細內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由
這篇文章主要介紹了Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由的方法,幫助大家實現(xiàn)路由信息的自動更新,感興趣的朋友可以了解下2020-10-10
JavaSwing FlowLayout 流式布局的實現(xiàn)
這篇文章主要介紹了JavaSwing FlowLayout 流式布局的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12

