0

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

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
user2490093
  • 69
  • 10

2 Answers2

1

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..!");
            });
});
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
  • Thank you very much.yes these two functions are stopped submitting two times and my another fault was caught : I changed both name and id with the value uName for username filed then it is not given me null value for username filed. – user2490093 Oct 03 '17 at 05:31
0

this button type is 'submit' so when you click it your form will be submit coherently but no url path LogIn

Stone Wu
  • 1
  • 2