kaptcha组件生成验证码

    xiaoxiao2021-03-25  116

    kaptcha组件生成验证码

    先导入kaptcha的jar包

    WebRoot---webinf----lib

    web.xml 配置------------------------------------------------------------------------------------------

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">   <display-name></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.jsp</welcome-file>   </welcome-file-list> </web-app>

    jsp页面

    login页

    <%@ page language="java" import="java.util.*" pageEncoding="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" method="post">   <img src="randomcode.jpg"> <input type="text" name="login"/> <input type="submit" value="提交"/>     </form>   </body> </html>

    check页面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%   String k=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);     String strLogin=request.getParameter("login");     if(strLogin.equals(k)){     out.println("true");     }else{     out.println("false");     }     out.println(k+"----"+strLogin);  %>

    中文验证码---以后补

    算式验证码---以后补

    转载请注明原文地址: https://ju.6miu.com/read-12626.html

    最新回复(0)