user2490093 user2490093 - 1 year ago 95
Ajax Question

Form submit for two times with null values and given me 500 error

I created a login form which as I wanted to practice ajax from JQUERY. Unfortunately this program is given me unexpected error.


  • ISSUE
    Browser is given me 500 error : NullPointerException
    So I printed username and password. Then I saw that for one button click username and password print for two times and for first time it is same values as I entered and second time username is Null password is similar to entered value.And other thing is although I commented out the Ajax part that second scenario is happening (null username and entered password printing)



JSP:

<form action="" style="border:1px solid #ccc" method="post">
<div class="container">
<h2>LogIn Form</h2>
<label><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" id="uName" required>

<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" id="psw" required>

<div class="clearfix">
<button type="button" class="cancelbtn">Cancel</button>
<button type="submit" class="signupbtn" id="login">LogIn</button>
</div>
</div>
</form>



  • JQUERY

    <script type="text/javascript">
    $(document).ready(function() {
    alert("qwqw");
    $('#login').click(function(){
    $.post('userRegistrationServlet',{
    uName : $('#uName').val(),
    psw : $('#psw').val()},
    function(responseText) {
    alert(uName);
    alert("Login Successfully..!");
    });
    });
    });
    </script>

  • Servlet

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    //PrintWriter out = response.getWriter();

    String userName = request.getParameter("uName");
    System.out.println(userName+"uName");
    String psw = request.getParameter("psw");
    System.out.println(psw+"psw");
    RequestDispatcher rd;
    if(!userName.isEmpty()| userName != null){
    rd = request.getRequestDispatcher("/Header.html");
    rd.forward(request, response);
    }else{
    rd = request.getRequestDispatcher("/SignUp.jsp");
    rd.forward(request, response);
    }

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException{
    request.getRequestDispatcher("UserRegistration.jsp").forward(request,response);
    }



Please help me to solve this issue..Thank you..!

Answer Source

I'd rather put an event handler on the "submit" event of the form, then call preventDefault() and stopPropagation() on the event:

$("#myform").on("submit", function(e) {
    e.preventDefault();
    e.stopPropagation();
    $.post('userRegistrationServlet',{
            uName : $('#uName').val(),
            psw : $('#psw').val()},
            function(responseText) {
                alert(uName);
                alert("Login Successfully..!");
            });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download