I am facing an issue with a form on a jsp that calls a javascript function, which in turn calls a servlet. The problem is that the code works intermittently, and when it does reach the servlet, the parameters come back as null. It's also strange that it alternates between using the doGet and doPost methods even though I have specified "POST". Can someone help me troubleshoot this?
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 you need any other information or further clarification, please let me know. Thank you for your assistance.