您的位置:首页 > 其它

kaptcha组件生成验证码

2017-03-09 15:45 369 查看
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);

 %>

中文验证码---以后补

算式验证码---以后补
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: