点图片的时候验证码不刷新,点网址旁边的刷新就可以还有输入错误的验证码登录没反应,我是用eclipse

来源:5-1 完成验证功能

Fandy0706

2018-04-11 15:52:10

http://img.mukewang.com/climg/5acdbc8d0001279903200157.jpg

http://img.mukewang.com/climg/5acdbd1600011a5211890829.jpg

http://img.mukewang.com/climg/5acdbd480001c96407810775.jpg

写回答

3回答

一叶知秋519

2018-04-12

我把你贴出来的代码替换到源码中去运行,验证码是可以刷新的;验证码输入错误,点击登录后,也是有提示的。建议同学通过浏览器的F12审查元素查看一下,你刷新的时候,看下浏览器后台有没有出现错误。祝学习愉快!

0

Fandy0706

提问者

2018-04-12

public class LoginServlet extends HttpServlet {

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

        //设置编码

        request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");


        //获取浏览器输出流对象

        PrintWriter out = response.getWriter();

        //获取用户传递过来的验证码

        String code = request.getParameter("code");

        //获取验证码框架产生的验证码(会话中存储的验证码)

        String sessionCode = (String)request.getSession().getAttribute("Constants.KAPTCHA_SESSION_KEY");

        if(code!=null&sessionCode!=null) {

            //如果用户输入的验证码和产生在服务器端的验证码一致,那么就告诉用户输入正确

            if (code.equalsIgnoreCase(sessionCode)) {

                //登录逻辑、注册逻辑等相关的业务操作

                out.print("success");

            } else {

                out.print("fail");

            }

        }

        out.flush();

        out.close();



    }


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

        this.doPost(request,response);

    }

}


<form action="submit.action">


    <p> <input type="text" name="kaptcha" id="code" value="" maxlength="4" placeholder="请输入验证码"/>

    <img src="http://localhost:8080/CourseMent/kaptcha.jpg" id="changecode"/>

    </p>

    <p>

    <input type="button" id="login" value="登录">

    </p>

    <div id="result"></div>

</form>


<script src="js/jquery-1.12.4.min.js" type="text/javascript"></script>

<script>


    $(function(){

        $("#changecode").on("click",function(){

            $(this).attr("src","http://localhost:8080/CourseMent/kaptcha.jpg?d="+new Date().getTime());

        });

         //给登录按钮绑定点击事件

        $("#login").on("click",function(){

            //获取用户输入的验证码

            var code = $("#code").val();

            //alert(code);

            var params = {"code":code};

            $.post("http://localhost:8080/CourseMent/login",params,function(data){

//                if(data=="fail"){

//                     alert("验证码输入有误!");

//                }

                if(data=="success"){

                    $("#result").html("验证码输入正确");

                }else{

                    $("#result").html("验证码输入有误,请重新输入...");

                    $("#code").val("").focus();

                }

            });

        });

    })


</script>


0

一叶知秋519

2018-04-11

你到Chrome浏览器或者火狐浏览器下再试下,另外建议同学将代码贴出来,不要截图,方便帮助你解答。祝学习愉快!

0
handy0706
h 换了火狐还是不行啊老师
h018-04-12
共1条回复

0 学习 · 1363 问题

查看课程