用kaptcha生成的图片显示的验证码和实际获取到的验证码不一致

用kaptcha生成的图片显示的验证码和实际获取到的验证码不一致

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

如图所示,图片里显示的是gaey。

可是我的程序输出的却不是这样的,我的程序输出如下:

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

上图中前者是我input输入框的值,而后者是通过下面这句程序获得的值:

1
String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

我搞不清这是怎么一回事,希望有老师能帮忙分析分析。

下面上相关的代码:

  1. web.xml文件

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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    id="WebApp_ID" version="4.0">
 
    <!-- 初始化InitServlet -->
    <servlet>
        <servlet-name>InitServlet</servlet-name>
        <servlet-class>com.imooc.servlet.InitServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>InitServlet</servlet-name>
        <url-pattern>/InitServlet</url-pattern>
    </servlet-mapping>
 
    <!-- 字符编码过滤器 -->
    <filter>
        <filter-name>EncodeFilter</filter-name>
        <filter-class>com.imooc.filter.EncodeFilter</filter-class>
        <init-param>
            <param-name>charset</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>EncodeFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!-- 配置kaptcha -->
    <servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
        <init-param>
            <param-name>kaptcha.image.width</param-name>
            <param-value>120</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.image.height</param-name>
            <param-value>50</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.font.size</param-name>
            <param-value>40</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.char.length</param-name>
            <param-value>4</param-value>
        </init-param>
    </servlet>
     
    <servlet-mapping>
        <servlet-name>Kaptcha</servlet-name>
        <url-pattern>/kaptcha.jpg</url-pattern>
    </servlet-mapping>
     
    <!-- 配置LoginServlet -->
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.imooc.servlet.LoginServlet</servlet-class>
    </servlet>
     
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
 
</web-app>

2.LoginServlet.java

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
package com.imooc.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.imooc.service.CourseServiceImpl;
 
/*
 * 控制登录的Servlet
 */
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //校验验证码是否正确
        //从session中取出servlet生成的验证码text值
        String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        //获取用户页面输入的验证码
        String kaptchaReceived = request.getParameter("checkCode");
        System.out.println(kaptchaReceived + "===" + kaptchaExpected);
        if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)){
            //如果验证失败,弹出弹窗提示:验证码错误
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language='javascript'>alert('验证码错误');window.location.href='index.jsp';</script>");
            return;
        }
        //获取到登录页面提交的用户名和密码信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //判断数据库中是否存在用户信息
        CourseServiceImpl courseServiceImpl = (CourseServiceImpl) this.getServletContext().getAttribute("courseServiceImpl");
        if(courseServiceImpl.login(username, password)) {
            //如果登录成功则在session中设置loginUser属性,并重定向到server.jsp页面
            HttpSession session = request.getSession();
            session.setAttribute("loginUser", username);
            response.sendRedirect(request.getContextPath() + "/server.jsp");
        }else {
            //如果登录失败,弹出弹窗提示:用户名或密码错误
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language='javascript' charset='utf-8'>alert('用户名或密码错误');window.location.href='index.jsp';</script>");
        }
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
 
}

3.index.jsp

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
<%@ 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() {
        var inputCode = document.getElementById("inputCode").value;
        document.getElementById("code").src = "http://localhost:8080/class_management/kaptcha.jpg?d=" new Date().getTime();
    }
</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>


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

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

1回答
好帮手慕阿莹 2018-09-03 11:31:41

老师在 http://class.imooc.com/course/qadetail/66756  这个问答下提供了一种验证方法,建议同学尝试用这个方案试一下。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕数据4154996 #1
    后来我重新打开项目这个问题就自己解决了,也不知道为什么,谢谢老师提供的验证方法!
    2018-09-04 20:58:17
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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