验证码不正确还是会跳转到LoginServlet

验证码不正确还是会跳转到LoginServlet

我想做到验证码错误不跳转应该怎么实现,代码如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
   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">
   a 
   {
    text-decoration:none;
    font-size:12px;
    color:#288bc4;
   }
   a:hover 
   {
    text-decoration:underline;
   }
   img{
      margin:auto 0px;
   }
</style>

</head>
<body>
   <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位字母数字或下划线组合"></td>
            </tr>
            <tr>
               <td>密&nbsp;码</td>
               <td  colspan="2"><input type="password" name="password" placeholder="长度为5-12位字母数字或下划线组合" ></td>
            </tr>
            <tr>
               <td>验证码</td>
               <td style="border-right-style:none;">
                  <input type="text" name="checkCode" maxlength="4" placeholder="请输入验证码" id="code">
                        <img src="<%=basePath%>/kaptcha.jpg" id="changecode"/>
               </td>
            </tr>
            <tr>
               <td colspan="3" style="text-align:center">
                  <input type="submit" value="登录" id="login">
                  <input type="reset" value="取消">
               </td>
            </tr>
         </table>
      </form>
   </center>
   <script src="js/jquery-1.12.4.min.js"></script>
   <script>
        $(function () {
            $("#changecode").on("click",function () {
                $(this).attr("src","<%=basePath%>/kaptcha.jpg?d="+new Date().getTime());
            });
            
            $("#login").on("click",function () {
            var code = $("#code").val();
            var params = {"code":code};
            $.post("<%=basePath%>/LoginServlet",params,function (data) {
                if (data=="fail") {
                        alert("验证码输入有误");
                        $("#code").val("").focus();
                    }
                });
            });
        });

   </script>
</body>
</html>

 LoginServlet

package com.imooc.servlet;

import com.google.code.kaptcha.Constants;
import com.sun.tools.javac.code.Attribute;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //check code
        response.setCharacterEncoding("UTF-8");

        PrintWriter out = response.getWriter();
        String code = request.getParameter("checkCode");
        String sessionCode = (String) request.getSession().getAttribute("kcode");

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

            }
        }else {
            out.print("fail");
        }

        out.flush();
        out.close();

        //


    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!--验证码-->
    <servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
        <init-param>
            <param-name>kaptcha.border</param-name>
            <param-value>no</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.image.width</param-name>
            <param-value>100</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.image.height</param-name>
            <param-value>20</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.font.size</param-name>
            <param-value>16</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.char.string</param-name>
            <param-value>qwertyuiopasdfghjklzxcvbnm123456789</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.char.length</param-name>
            <param-value>4</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.noise.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.NoNoise</param-value>
        </init-param>
        <init-param>
            <!--session.setAttribute("kcode",生成好的验证吗)-->
            <param-name>kaptcha.session.key</param-name>
            <param-value>kcode</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.font.color    </param-name>
            <param-value>blue</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.obscurificator.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>Kaptcha</servlet-name>
        <url-pattern>/kaptcha.jpg</url-pattern>
    </servlet-mapping>
    <!--验证码-->

    <!--编码过滤-->
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.imooc.filter.EncodingFilter</filter-class>
        <init-param>
            <param-name>charset</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--编码过滤-->

    <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>


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

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

3回答
chrismorgen 2018-07-18 16:05:11

你获取用户的验证码时,应该是code而不是checkCode。如果我的建议解决了你的问题,请采纳,祝学习愉快~

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

提问者 KAGITO 2018-07-18 12:03:54
chrismorgen 2018-07-18 11:03:55

根据你的代码是要跳转到LoginServlet中的,如下图所示,可以在LoginServlet添加一个跳转的操作,如果验证码失败,就跳转到Login.jsp页面进行重新输入。祝学习愉快~

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

  • 提问者 KAGITO #1
    我的代码貌似有点问题,验证码输对了也是fail,然后code = request.getParameter("checkCode") 一开始为null,截图放上面了,貌似code和sessioncode都打印了两次
    2018-07-18 12:05:13
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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