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;
}
}