用Kaptcha第三方包做验证码验证

Kaptcha是google提供的一个第三方的包,可以做验证码验证。他是运行原理把验证码存放到session域中,当用到的时候,从session域中取出来,然后与输入的验证码进行对比,就可以了。接下来,来看代码。

首先,先导入kaptcha-2.3.2.jar包。之后再web.xml中配置验证码需要的信息。我先列举一下都可以配置什么信息。

kaptcha.border
验证码图片的边框,可以设置yes或者no
默认值 yes

kaptcha.border.color
边框的颜色reg值。合法值 rgb,black,blue,white
默认值 black

kaptcha.border.thickness
边框的宽度
默认 1

kaptcha.image.width
图片的宽度
默认200

kaptcha.image.height
图片的高度
默认50

kaptcha.producer.impl
生成图片使用的类
默认 com.google.code.kaptcha.impl.DefaultKaptcha

kaptcha.textproducer.impl
生成图片中文字的使用的类
默认com.google.code.kaptcha.text.impl.DefaultTextCreator

kaptcha.textproducer.char.string
验证码中使用的字符
默认 abcde2345678gfymnpwx

kaptcha.textproducer.char.length
验证码中字符的数量
默认 5

kaptcha.textproducer.font.names
验证码的字体
默认 Arial, Courier

kaptcha.textproducer.font.size
字体的大小
默认 40

kaptcha.textproducer.font.color
字体颜色 rgb值,颜色单词
默认 black

kaptcha.textproducer.char.space
两个字符之间的间距
默认 2

kaptcha.noise.impl
干扰线生成类
默认 com.google.code.kaptcha.impl.DefaultNoise

kaptcha.noise.color
干扰线颜色
默认 black

kaptcha.obscurificator.impl
The obscurificator implementation.
默认 com.google.code.kaptcha.impl.WaterRipple

kaptcha.background.impl
背景颜色设置类
默认 com.google.code.kaptcha.impl.DefaultBackground

kaptcha.background.clear.from
渐变颜色 左到右
默认 light grey

kaptcha.background.clear.to
渐变颜色 右到左
默认 white


kaptcha.word.impl
词语渲染器
默认 com.google.code.kaptcha.text.impl.DefaultWordRenderer


kaptcha.session.key
在session中存储属性的名字
默认 KAPTCHA_SESSION_KEY


web.xml的配置

<!-- 验证码的配置 -->
  <servlet>
    <servlet-name>KaptchaServlet</servlet-name>
    <!-- 引入类 -->
    <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
    
    <!-- 验证码显示的所有数据 -->
    <init-param>
      <param-name>kaptcha.textproducer.char.string</param-name>
      <param-value>ABSADJKLJsadasdFEIF187987</param-value>
    </init-param>
    
    <!-- 验证码中将会显示几个字母 -->
    <init-param>
      <param-name>kaptcha.textproducer.char.length</param-name>
      <param-value>4</param-value>
    </init-param>
    
    <!-- 验证码中字体的格式 -->
    <init-param>
      <param-name>kaptcha.textproducer.font.names</param-name>
      <param-value>楷体</param-value>
    </init-param>
    
    <!-- 验证码放入session中的key值 -->
    <init-param>
    	<param-name>kaptcha.session.key</param-name>
    	<param-value>code</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>KaptchaServlet</servlet-name>
    <url-pattern>/code.jpg</url-pattern>
  </servlet-mapping>

JSP中的代码:当第几图片是会刷新验证码

<script type="text/javascript" src="${pageContext.request.contextPath }/script/jquery-1.7.2.js"></script>
<script type="text/javascript">
	$(function(){
		$("#img_code").click(function(){
			this.src = "code.jpg?t="+Math.random();
		});
	});
</script>
</head>
<body>
	<form action="CodeServlet" method="post">
		请输入验证码:<input type="text" name="code"/><br><br>
		<img alt="" src="code.jpg" id="img_code"><br><br>
		<input type="submit" value="验证"/>
	</form>
</body>

Servlet中的代码

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取session对象
		HttpSession session = request.getSession();
		//获取前台输入框输入的验证码的值
		String imgCode = request.getParameter("code");
		//获取session中的验证码的值
		String code = (String) session.getAttribute("code");
		System.out.println("img_code:"+imgCode);
		System.out.println("code:"+code);
		//验证码进行对比
		if(code.equalsIgnoreCase(imgCode)){
			response.sendRedirect(request.getContextPath()+"/success.jsp");
		}else{
			response.sendRedirect(request.getContextPath()+"/error.jsp");
		}
	
	}

效果如下

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值