2016-04-29 29 views
0

所以,我目前有一个问题,我有一个表单,其中包含两个用户名和密码输入,一个验证码(<div>)和 submit类型的行为作为一个按钮。当这个提交按钮被击中时,我希望它检查验证码以查看它是否为空。如果它不是空的,我希望它调用验证用户的其他Java代码。如何调用与jsp相同形式的java代码和javascript代码

我的JSP形式如下:

<html> 
    <head> 
    <!-- more scripts/google api js are here --> 
    <script type="text/javascript"> 
     function get_action(form) { 
     var v = grecaptcha.getResponse(); 
     if(v.length === 0) { 
      document.getElementById('captcha').innerHTML="Login failed: Empty captcha"; 
      return false; 
     } else { 
      return true; 
     } 
     } 
    </script> 
    </head> 
    <body> 
    <form action="login" method="post" onsubmit="return get_action(this);"> 
     <input type="text" id="email" value="${fn:escapeXml(param.email)}" required> 
     <input type="text" id="password" value="${fn:escapeXml(param.password)}" required> 
     <div class="g-recaptcha" data-sitekey="xxx"></div> 
     <input class="submit_button" type="submit" name="submit" value="Submit" /> 
     <span class="error"${error.invalid}</span> 
     <div id="captcha" class="captchaError"></div> 
    </form> 
    </body> 
</html> 

,这是应该做用户的验证如下:我登录的servlet:

@WebServlet("/login") 
public class LoginServlet extends HttpServlet { 

    private LoginDAO loginDAO; 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
     request.getRequestDispatcher("login.jsp").forward(request,response); 
    } 

    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
     Map<String, String> error = new HashMap<String,String>(); 
     request.setAttribute("error",error); 

     String email = request.getParameter("email"); 
     String password = request.getParameter("password"); 

     // Verify re-captcha 
     String gRecaptchaResponse = request.getParameter("captcha"); 
     boolean verify = VerifyRecaptcha.verify(gRecaptchaResponse); 
     if(!verify) { 
      error.put("captcha","You seem to be a robot. Try to use the captcha again"); 
     } 
     if(error.isEmpty()) { 
      loginDAO = new LoginDAO(); 
      try { 
       List<Customer> customer = new ArrayList<Customer>(); 
       customer = loginDAO.validate(email,password); 
       if(customer.isEmpty()) { 
        error.put("invalid","Invalid email or password"); 
       } 
       if(error.isEmpty()) { // no errors, proceed 
        HttpSession session = request.getSession(true); 
        Customer user = customer.get(0); 
        session.setAttribute("user",user); 
        response.sendRedirect("main"); 
        return; 
       } 
       request.getRequestDispatcher("login").forward(request,response); 
      } catch(SQLException e) { 
       throw new ServletException("Could not check login",e); 
      } 
      loginDAO.closeLoginDAO(); 
     } 
    } 
} 

凡loginDAO只检查用户名和针对数据库的密码。

什么是奇怪的是,昨晚一切工作正常。我做的唯一的事情就是将java文件移动到子目录中。但我更新了web.xml文件以确保没有错误。

现在我有一种感觉formonsubmit=部分正在搞java类。有谁知道用JavaScript和java进行表单验证是正确的吗?我不幸的是需要一些JavaScript的reCaptcha,否则用户可以提交一个空的验证码。

+0

在'doGet()'中,您转发到'login.jsp'。在'doPost()'你转发到'登录'。不知道这是否是问题中的错字,但可能会导致问题。附注:在进行转发或重定向之前请注意关闭您的loginDAO资源。 – Tap

回答

0

在您的jsp文件var v = grecaptcha.getResponse();中,您失去了grecaptcha的定义,这会导致错误。

+0

它似乎没有影响任何东西。我的意思是它仍然是空白页面。在chrome中检查页面时不会显示javascript部分的任何错误。 – Alex