表单提交之前如何校验验证码

表单提交之前如何校验验证码

先上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String basePath = request.getScheme() + ":" "//" + request.getServerName() + ":" + request.getServerPort()
            + request.getServletContext().getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录页面</title>
<style type="text/css">
.code {
    cursor: pointer;
}
 
a {
    text-decoration: none;
    font-size: 12px;
    color: #288bc4;
}
 
a:hover {
    text-decoration: underline;
}
</style>
<script>
    //刷新验证码
    function changeCode() {
        document.getElementById("code").src = "http://localhost:8080/class_management/kaptcha.jpg?d=" new Date().getTime();
    }
    //校验验证码
    function validateCode(){
 
    }
</script>
</head>
<body">
    <center>
        <h1>用户登录</h1>
        <form action="<%=basePath%>/LoginServlet" method="post" onsubmit="return validateCode()">
            <table width="400px" height="200px" cellspacing="0px" cellpadding="0px" border="1px">
                <tr>
                    <td>用户名</td>
                    <td colspan="2">
                        <input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合"
                                pattern="[a-zA-Z_0-9]{3,12}" required="required" >
                    </td>
                </tr>
                <tr>
                    <td>密&nbsp;码</td>
                    <td colspan="2">
                        <input type="password" name="password" placeholder="长度为5-12位字母数字或下划线组合" 
                                pattern="[a-zA-Z_0-9]{5,12}" required="required">
                    </td>
                </tr>
                <tr>
                    <td>验证码</td>
                    <td style="border-right-style: none;">
                        <input type="text" name="checkCode" placeholder="请输入验证码" id="inputCode">
                    </td>
                    <td style="border-left-style: none;">
                        <div class="code" id="checkCode">
                            <img src="http://localhost:8080/class_management/kaptcha.jpg" id="code" onclick="changeCode()" />
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="3" style="text-align: center">
                    <input type="submit" value="登录"> <input type="reset" value="取消">
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

http://img1.sycdn.imooc.com//climg/5b8bddc10001e6e918241117.jpg

我的想法是在js脚本中用validateCode()的返回值来校验验证码是否正确。可是校验验证码得用java代码来写啊,如下图所示。

1
2
3
4
5
6
7
8
9
Java代码  收藏代码
//从session中取出servlet生成的验证码text值
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脚本中写java代码啊?请问老师这个该怎么实现呢?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
一叶知秋519 2018-09-03 11:09:09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录页面</title>
<style type="text/css">
    .code 
    {
     background:url(code_bg.jpg);
     font-family:Arial;
     font-style:italic;
     color:blue;
     font-size:20px;
     border:0;
     padding:2px 3px;
     letter-spacing:3px;
     font-weight:bolder;
     float:left;
     cursor:pointer;
     width:40x;
     height:20px;
     line-height:20px;
     text-align:center;
     vertical-align:middle;
    }
    
    {
     text-decoration:none;
     font-size:12px;
     color:#288bc4;
    }
    a:hover 
    {
     text-decoration:underline;
    }
</style>
<script type="text/javascript">
    var code;
    function createCode() 
    {
     code = "";
     var codeLength = 4//验证码的长度
     var checkCode = document.getElementById("checkCode");
     var codeChars = new Array(0123456789
          'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
          'A''B''C''D''E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S''T''U''V''W''X''Y''Z'); //所有候选组成验证码的字符,当然也可以用中文的
     for(var i = 0; i < codeLength; i++) 
     {
      var charNum = Math.floor(Math.random() * 52);
      code += codeChars[charNum];
     }
     if(checkCode) 
     {
      checkCode.className = "code";
      checkCode.innerHTML = code;
     }
    }
    function validateCode() 
    {
     var inputCode=document.getElementById("inputCode").value;
     if(inputCode.length <= 0
     {
      alert("请输入验证码!");
      return false;
     }
     else if(inputCode.toUpperCase() != code.toUpperCase()) 
     {
       alert("验证码输入有误!");
       createCode();
       return false;
     }
     else 
     {
      //alert("验证码正确!");
      return true;
     }    
    }  
</script>
</head>
<body onload="createCode()">
    <center>
        <h1>用户登录</h1>
        <form action="<%=basePath%>/LoginServlet" method="post" onsubmit="return validateCode()">
            <table width="300px" cellspacing="0px" cellpadding="0px" border="1px">
                <tr>
                    <td>用户名</td>
                    <td colspan="2"><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" pattern="\w{3,12}" required="required"></td>
                </tr>
                <tr>
                    <td>密&nbsp;码</td>
                    <td  colspan="2"><input type="password" name="password" placeholder="长度为5-12位字母数字或下划线组合" pattern="\w{5,12}" required="required"></td>
                </tr>
                <tr>
                    <td>验证码</td>
                    <td style="border-right-style:none;">
                        <input type="text" name="checkCode" placeholder="请输入验证码" id="inputCode">
                    </td>
                    <td style="border-left-style:none;"><div class="code" id="checkCode" onclick="createCode()" ></div></td>
                </tr>
                <tr>
                    <td colspan="3" style="text-align:center">
                        <input type="submit" value="登录">
                        <input type="reset" value="取消">
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

同学可以参考上面代码尝试完成一下~

祝学习愉快 !

  • 提问者 慕数据4154996 #1
    那用kaptcha是不是一定得提交到后台才能验证呢?
    2018-09-04 20:58:53
  • 慕布斯37364 回复 提问者 慕数据4154996 #2
    https://blog.csdn.net/abc19900828/article/details/40895201 你可以看看这个博客,他好像是在前台界面写的代码进行验证的。
    2018-09-05 10:11:15
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码