0

First quesion here. Inside a JSP page I have a form. What I wanna do is ask the servlet if an input value of that form is already in my mysql database or not. I'm working with 2 different Servlets. "ServletInsert" inserts data into the mysql database. "ServletCheck" checks if the ticket id exists on the mysql database. The ticket id can't be autogenerated.

JSP page:

<form id="myForm" action="ServletInsert" method="post">        
     <div>
     <label>Ticket Id:</label>    
     <input type="text" name="id"/>
     </div>           
     <input type="submit" name="submit" value="Send" />    
</form>   
<script>
    $('#myForm').submit(function() {
       /** <<SEND INPUT VALUE("id") TO SERVLET("ServletCheck")>> */
       if ("the answer from the servlet is true"){
       return true;
       }else if ("the answer from the servlet is false")
       return false;
       alert("ticket id already exists");
       }
    }); 
</script> 

Servlet ("ServletCheck"):

package Servlets;    
import Entity.Ticket;  

public class ServletCheck extends HttpServlet {    

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
    processRequest(request, response);

    String id = request.getParameter("id"); /**the input value*/
    ArrayList<Ticket> list = new ArrayList<Ticket>();    
    list = ListOfTicketsDB(); /**this procedure gets the list of tickets from 
                         the mysql database (already know how to do that)*/
    boolean b = existsOnList(list, id); 
    /**The answer doesn't have to be boolean. It can be a String("True","False")*/

    /**<<SEND THE ANSWER TO THE JSP PAGE>>*/
} 

/**checks if the value is inside the array*/
boolean existsOnList(ArrayList<Ticket> list, String id) {
    for (Ticket T : list) {
        if (T.getID_TICKET().equals(id)) {
            return true;
        }
    }
    return false;
   }    
}
Nicholas
  • 41
  • 1
  • 4

1 Answers1

0

To complete this as you're doing it, you would want to include something like this:

/**<<SEND THE ANSWER TO THE JSP PAGE>>*/

response.setContentType("application/text");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();    

if(existsOnList(list, id)){

    out.write("False");

}else{

    out.write("True");

}

Then on your JSP, you would want something like (using AJAX):

$('#myForm').submit(function(e) {

    $.get('ServletCheck', $('#myForm').serialize(), function (response) {

       if (response === "True"){

           return true;

       }else if (response === "False"){

           e.preventDefault(); //cancel form submission
           return false;
           alert("ticket id already exists");

       }

    }

});
  • It didn't work. The form is still submitting as if the ajax code didn't exist. – Nicholas Aug 09 '17 at 00:13
  • After some tests, I come to the conclusion that the servlet 'ServletCheck' works fine and the function " $.get('ServletCheck', $('#myForm').serialize(), function (response) " gets the apropiate answer. The only problem is that "e.preventDefault()" and "return false" don't stop the form from submitting. Although, the line "return false" does work and stops the submission ONLY WHEN IT'S OUTSIDE this: "$.get(.......,funcion(response) { }". – Nicholas Aug 09 '17 at 04:14