验证码中的验证码代码值是否为null?

时间:2014-05-09 10:34:30

标签: java javascript jquery servlets captcha

表单代码:

<form class="form"  name ="custRegistration"  id="custRegistration"  onsubmit="return submitAlbum(this)" action="download.jsp" method="post" >

        <p class="Captcha" style="margin-top:5%; width: 100%;">
        <div style="margin:0 0 0 1%">
        <label class="captchalabel" style="width:38%; float: left;"><span style="margin:0 0 0 9%">Enter the text </span>  <span style="color:red">*</span>:</label>
        <input style=" float: left;" type="text" name="defaultReal" required>       </div>
        <div style="width: 20%; float: right;"  id="defaultReal">
        </div>
        </p>

</form>

这是对js文件的调用:

<script type="text/javascript" src="js/jquery.realperson.js"></script>
<script type="text/javascript">
$(function() {
    var j = jQuery.noConflict();
    j('#defaultReal').realperson();
});
</script>

来自js的主要功能:这里是&#39; hashNmae&#39;应该与servlet中的验证码进行比较。

function RealPerson() {
    this._defaults = {
        length: 6, // Number of characters to use
        includeNumbers: true, // True to use numbers as well as letters
        regenerate: '<div style="margin: -18% 1% 0 80%;"><img src="images/captcha.png"/></div>', // Instruction text to regenerate
        hashName: '{n}Hash' // Name of the hash value field to compare with,
            // use {n} to substitute with the original field name
    };
}

servlet代码:

public class CustomerinfoServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("inside doGet function");
        System.out.println("default real hash=="+request.getParameter("defaultRealHash"));
        System.out.println("rpHash value==="+rpHash(request.getParameter("defaultReal")).equals(request.getParameter("defaultRealHash")));

        if (rpHash(request.getParameter("defaultReal")).equals(request.getParameter("defaultRealHash"))) {
            // Accepted
            System.out.println("hello this is accepted");

        }
        else {
            // Rejected
            System.out.println("hello this is rejected");

        }



    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
        HttpSession session =request.getSession();     
    }


    private String rpHash(String value) {
        int hash = 5381;
        value = value.toUpperCase();

        for(int i = 0; i < value.length(); i++) {
            hash = ((hash << 5) + hash) + value.charAt(i);
        }
        System.out.println("hello this is value"+String.valueOf(hash));
        return String.valueOf(hash);
    }

}

供参考:

http://keith-wood.name/realPerson.html

我使用过简单和酷的jQuery CAPTCHA插件 - 真人。但是在我输入任何文本的代码中,它显示被拒绝。 我在servlet中实现了文本验证。我是它的输出:

inside doGet function
default real hash==null
hello this is value-1058740979
rpHash value===false
hello this is value-1058740979
hello this is rejected

0 个答案:

没有答案