0

I have the following jQuery statement running in a .jsp file in Eclipse, located in the WebContent/public directory:

        $(document).ready(function(){
        $('#login').click(function(){
            $('#display').html('Logging in...');
            var username = $('#username').val();
            var password = $('#password').val();
            $.ajax({
                url : '/AuthenticationServlet',
                type : 'POST',
                dataType : 'text',
                data : {'username' : username, 'password' :     password},
                success : function(data) {
                    $('#display').html('');
                    if(data != "SUCCESS"){
                        //TODO create an element to display validity
                        $('#display').html('Invalid Username/Password combination');
                    }else if (data == "SUCCESS"){
                        $('#username').val('');
                        $('#password').val('');
                        $('#display').html('Success!');                 
                    }   
                    else{
                        $('#display').html('Something has gone horribly wrong.');
                    }

                }
            });
            return false;
        });
    });

I have my servlet, AuthenticationServlet, in the src/authentication directory, and it is written as follows:

    /**
    * Servlet implementation class AuthenticationServlet
 */
@WebServlet("/AuthenticationServlet/*")
public class AuthenticationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public AuthenticationServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     * This method gets the username and password from the jsp file, then proceeds to pass them to
     * the authentication helper for validation.
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get the parameters from the request
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //verify the login/password combination is correct

        boolean authenticated = AuthenticationHelper.verifyUser(username, password);

        if(authenticated){
            response.getWriter().write("SUCCESS");
        }
        else{
            response.getWriter().write("FAILURE");
        }
    }
}

The thing is, upon entering a valid username and password combination, my 'display' element in the JSP only changes to "Logging in" and doesn't change at all, signifying that the jquery isn't executing any more of the code that would end up changing the element to something else (whether it be a blank, or any of the other strings). What am I doing wrong? Is my URL not correct?

1
  • try adding error: function(){ alert('error'); } too, see if this alert comes up, if yes then try after removing the dataType:"text". Commented Nov 14, 2013 at 8:01

1 Answer 1

1

As you want to send request as username=usernameValue&password=passwordValue. So, modify url and data as below:

  url : 'AuthenticationServlet', // Remove /, it will skip the context path.
  data : {username : username, password : password}, // Remove quotation.
Sign up to request clarification or add additional context in comments.

1 Comment

this gives a 404, or at least on localhost it does

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.