javaWeb使用Kaptcha组件生成验证码
程序员文章站
2024-03-31 20:39:10
javaweb之使用kaptcha组件使用验证码
web.xml:
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); %>
结果:
验证码部分属性修改说明:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。