js判断字符串是否包含某个字符串|jsp 用 kaptcha 插件生成数字运算图形验证码

更新时间:2019-12-01    来源:Google    手机版     字体:

【www.bbyears.com--Google】

1.从官网https://code.google.com/p/kaptcha/下载kaptcha压缩文件,解压文件后里面有一个war文件,打开Eclipse/MyEclipse将其import进去,然后部署到服务器,在浏览器输入url即可看到kaptcha官方提供的基本demo的运行情况,现在将其改为加法计算验证。

2.首先查看web.xml文件发现用来生成验证码的servlet为KaptchaServlet

01.png

3.找到KaptchaServlet.class文件,然后进行反编译。

4.新建一个自己的验证码MyKaptchaServlet,将反编译得到的源码拷贝进来。

5.对MyKaptchaServlet做如下修改。

01.png

修改后MyKaptchaServlet.java代码如下:


package com.xhc.kaptchaServlet;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyKaptchaServlet extends HttpServlet implements Servlet {
 private Properties props;
 private Producer kaptchaProducer;
 private String sessionKeyValue;
 private String sessionKeyDateValue;

 public MyKaptchaServlet() {
  this.props = new Properties();

  this.kaptchaProducer = null;

  this.sessionKeyValue = null;

  this.sessionKeyDateValue = null;
 }

 public void init(ServletConfig conf) throws ServletException {
  super.init(conf);

  ImageIO.setUseCache(false);

  Enumeration initParams = conf.getInitParameterNames();
  while (initParams.hasMoreElements()) {
   String key = (String) initParams.nextElement();
   String value = conf.getInitParameter(key);
   this.props.put(key, value);
  }

  Config config = new Config(this.props);
  this.kaptchaProducer = config.getProducerImpl();
  this.sessionKeyValue = config.getSessionKey();
  this.sessionKeyDateValue = config.getSessionDate();
 }

 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  resp.setDateHeader("Expires", 0L);

  resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

  resp.addHeader("Cache-Control", "post-check=0, pre-check=0");

  resp.setHeader("Pragma", "no-cache");

  resp.setContentType("image/jpeg");

  String capText = this.kaptchaProducer.createText();

  String str1 = capText.substring(0, 2);
  String str2 = capText.substring(2, 4);
  int result = Integer.valueOf(str1) + Integer.valueOf(str2);
  req.getSession().setAttribute(this.sessionKeyValue, result+"");

  req.getSession().setAttribute(this.sessionKeyDateValue, new Date());

  BufferedImage bi = this.kaptchaProducer.createImage(str1 + "+" + str2
    + "=?");

  ServletOutputStream out = resp.getOutputStream();

  ImageIO.write(bi, "jpg", out);
 }
}


6.修改web.xml配置文件,将servlet-class指定到MyKaptchaServlet

01.png

参考官方文档

https://code.google.com/p/kaptcha/wiki/ConfigParameters将其他属性也配置进去,修改后web.xml文件如下:



xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">


Kaptcha

com.xhc.kaptchaServlet.MyKaptchaServlet



kaptcha.border
yes



kaptcha.border.color
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
01234565789



kaptcha.textproducer.char.length
4



kaptcha.textproducer.font.names
Arial,Courier



kaptcha.textproducer.font.size
40



kaptcha.textproducer.font.color
blue



kaptcha.textproducer.char.space
5



kaptcha.noise.impl
com.google.code.kaptcha.impl.DefaultNoise



kaptcha.noise.color
yellow



kaptcha.obscurificator.impl
com.google.code.kaptcha.impl.FishEyeGimpy



kaptcha.background.clear.from
gray



kaptcha.background.clear.to
white



kaptcha.word.impl
com.google.code.kaptcha.text.impl.DefaultWordRenderer


kaptcha.session.key
KAPTCHA_SESSION_KEY


kaptcha.session.date
KAPTCHA_SESSION_DATE




Kaptcha
/Kaptcha.jpg



KaptchaExample.jsp


7.效果图

01.gif

java自动生成验证码插件-kaptcha


 kaptcha一个很好用的验证码插件,java版的,很不错的一个插件,只用简单的在web.xml中设置几个属性,一个很漂亮的验证码就出来了。其他的参数都可以自己设置,最牛的就是提供了接口,可以自己定义哦。

html页面代码


   


web.xml配置代码


        Kaptcha
        com.google.code.kaptcha.servlet.KaptchaServlet


        Kaptcha
        /kaptcha.jpg


java 代码

String kaptchaExpected = (String)request.getSession()
    .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String kaptchaReceived = request.getParameter("kaptcha");
 
if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected))
{
    setError("kaptcha", "Invalid validation code.");
}

点击显示代码 刷新方法 js方法


<script type="text/javascript">
    $(function(){
        $('#kaptchaImage').click(function () {
            $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) );
        })
    });
</script>

Can't read the image? Click it to get a new one.

其他参数设置

 
            kaptcha.border  
            yes  
       
 
         
            kaptcha.border.color  
            105,179,90  
       

         
            kaptcha.textproducer.font.color  
            black  
       
 
         
            kaptcha.image.width  
            500  
       
 
         
            kaptcha.image.height  
            300  
       
 
         
            kaptcha.textproducer.font.size  
            90  
       
 
         
            kaptcha.session.key  
            code  
       
 
         
            kaptcha.textproducer.char.length  
            4  
       
 
         
            kaptcha.textproducer.font.names  
            宋体,楷体,微软雅黑  
       



本文来源:http://www.bbyears.com/seo/81646.html

热门标签

更多>>

本类排行