1

I have a form on a jsp which calls a javascript function which latter calls a servlet. However the code mention below works once in while and when the code does reach the servlet the parameters return null. Also its really bizarre but it jumps between The doGet and doPost method even though i specify "POST". Can someone assist me with the correction.

JAVASCRIPT:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
  $().ready(function() {
    $('.my_button').click(function() {

        var dataf = 'email=' + $('#email').val()
                + '&password=' + $('#password').val();
        $.ajax({
            url: "http://localhost:8080/RetailerGui/loginServlet",
            type: "POST",
            data: dataf,
            dataType: "JSON",
            success: function(data) {
            alert(data);

            }
        });

    });
});
</script>

JSP FORM:

              <form id="newsletter" method="POST">
                  <div class="wrapper">
                    <div class="bg">
                      Email:<input type="text" id="email">
                    </div>
                     <div class="bg">
                      Password:<input type="password" id="password">
                    </div>
                    <button class="my_button" name="login" >login</button>
                  </div>
                </form>

SERVLET "loginServlet":

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
       String email = request.getParameter("email");
        String password = request.getParameter("password");
        String loginResult = login(email,password);
        System.out.println("EMAIL:" +email);
        System.out.println("PASSWORD:" +password);
        System.out.println("IM INSIDE GET!!!!");
        response.getWriter().write(loginResult);

    }

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);

    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String loginResult = login(email,password);
    System.out.println("IM INSIDE POST!!!!");
    response.getWriter().write(loginResult);
}

If any other information is required please let me know. Thank you for the help in advance.

KSM
  • 262
  • 2
  • 6
  • 16
  • aren't you missing an action="" parameter? – onesixtyfourth Mar 17 '14 at 14:33
  • 2
    Your ` – Pointy Mar 17 '14 at 14:35
  • The [jQuery docs](http://api.jquery.com/ready/) say that `$().ready( handler )` is not recommended. Could this be a factor? The document seems to contradict itself by stating explicitly that "the selector can be omitted". – JimmyM Mar 17 '14 at 14:43
  • There are several problems with your code. You should actually use `` instead of ` – Powerslave Mar 17 '14 at 14:55
  • @Pointy,@JimmyM so i added type=button and removed $().ready( handler ) now the button seems to be inactive and it is not reaching the js funtion at all. – KSM Mar 17 '14 at 14:55
  • You should not have removed the "ready" handler. @Powerslave there's no reason to use `` if he's going to disable the native functionality anyway; a ` – Pointy Mar 17 '14 at 14:57
  • 1
    @JimmyM you're misrepresenting that advice. It's not about avoiding "ready" handlers entirely; it's saying that there's no need to write it that way when `$(function() { ... })` means exactly the same thing. – Pointy Mar 17 '14 at 14:59
  • @JimmyM sorry misunderstood. – KSM Mar 17 '14 at 15:01
  • @Pointy keeping the "ready" handler or not still makes the button response inactive for some reason with type="button" – KSM Mar 17 '14 at 15:05
  • Are you getting errors in the developer console? Can you tell whether the HTTP request is being made with appropriate arguments? What do you see from the server side? – Pointy Mar 17 '14 at 15:07
  • So i added a Alert("CLICKED"); within the button function in the js so with removing the "ready" handler it is not displayed, however keeping the "ready" handler with alert is displayed, for the server side console does not display anything, however sometimes when it does reach the console no error message just the email and password variables are assigned null. – KSM Mar 17 '14 at 15:16

2 Answers2

0

Three points here,

  1. In the html, your button will end up submitting your form to the current url (Refer https://stackoverflow.com/a/9824845/1304559)

    Set type="button" to change that. type="submit" is the default (as specified by the HTML recommendation).

  2. In the html, <form>, add onsubmit="return false". Just to be entirely safe that it does not post back to the current url.
  3. One question - what does processRequest do in your servlet code?
Community
  • 1
  • 1
aravind
  • 2,867
  • 17
  • 28
0

I Found the solution through this site, thank you for the help everyone! http://coderlearner.com/Java_Servlet_doGet_Ajax_JSON#LoginJSON.java

KSM
  • 262
  • 2
  • 6
  • 16