javaWeb使用Kaptcha组件生成验证码
作者:爸爸去哪了2之熊猫三胞胎
这篇文章主要为大家详细介绍了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); %>
结果:
验证码部分属性修改说明:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。