源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javaWeb使用Kaptcha组件生成验证码

  • 时间:2021-06-03 18:57 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javaWeb使用Kaptcha组件生成验证码
javaWeb之使用Kaptcha组件使用验证码 [img]http://files.jb51.net/file_images/article/201610/20161025161539327.jpg?2016925161549[/img]   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);
%>

结果: [img]http://files.jb51.net/file_images/article/201610/2016102516140541.png[/img] 验证码部分属性修改说明: [img]http://files.jb51.net/file_images/article/201610/2016102516140542.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140643.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140644.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140645.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140646.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140647.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140648.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140649.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140750.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140751.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140752.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140753.png[/img] [img]http://files.jb51.net/file_images/article/201610/2016102516140854.png[/img] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部