2

I want to add customers to a database using JSPs, JQuery and Servlets. When a customer already exists within the database table I want to show a confirmation message (Customer already exists. Are your sure you want to add the same customer?). When Customer does not exist all the data is written into the table and there is no need to show a confirm message.

My jquery code so far:

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

      var c = confirm("Customer have already. Are your sure you want to add the same customer?");
      return c;

});

And my servlet:

   int i = 0;
   ArrayList<Customer> cus = CustomerDao.addcustomer("abc");
   i = cus.size();
   if(i > 0){
        statement;
    }else{
        statement ;
    }

When I click the submit button it always shows the confirmation message. I don't wanna show confirmation message when the customer does not already exist in the database.

andih
  • 5,330
  • 3
  • 25
  • 35
Ma YongChhin
  • 357
  • 2
  • 9
  • 22

3 Answers3

0

You can write a cookie to the client side on submit and then you check if that cookie is there you show the message if not you go through normal processing

SwEngin
  • 91
  • 1
  • 3
0

Assuming that you have a jsp like login.jsp which posts to servlet e.g. myservlet which in turn calls a method to query to database. I would try the below :-

  1. Post the user provided values to the servlet myservlet (post method). Now use these values on a method which queries the database and finds whether this user already exists.

2.If yes , then use RequestDispatcher to the login.jsp . This time give a message that the user already exists. The user needs to fill the info again with new username post the values again.

  1. If the user doesn't exists , continue the normal flow and create the user.
sudhir
  • 36
  • 3
  • i understand about your answer. but when customer already exist i want to add more the same customer by show the confirmation message if click "yes" can insert the same customer, if click "no" cancel – Ma YongChhin Dec 20 '14 at 04:51
  • Ok...i would then suggest to use RequestDispatcher forward to a page like customerExists.jsp . This time ask the user whether he/she still wants to go ahead with user creation. If yes , then go ahead with inserting it . if no , redirect to login.jsp. – sudhir Dec 20 '14 at 05:03
  • However , you will have to make sure that you are not violating any database unique constraint rules. If you have made customer name as unique , certainly you would never achieve what you are trying to say here . But , if username is not defined as unique in your database , then go ahead but make sure that you have another attribute which verifies a user uniquely e.g. create a unique id for user or ask for email and make that a unique field. – sudhir Dec 20 '14 at 05:07
0

1)If you are using ajax for submitting your form data you can write responses and the same will be displayed, eg:-

//in servlet
if(userExists)//dummycode
 response.getWriter().println("User already exists");
 //above will get displayed on your html/jsp page
else
 //logic

2) if you are not using ajax, you can write your response in servlet in an attribute and print the response from that attribute in jsp, eg:-

//in servlet
request.setAttribute("Response","User already exists");
RequestDispatcher rd;
        rd = request.getRequestDispatcher("YourJSPpage");
        rd.forward(request, response);

and in jsp

<%
String msg=request.getAttribute("Response");
%>
if(msg!=null){
<p><%=msg%></p>
}

But as scriptlets in jsp are discouraged you can use JSTL tags eg:-

${requestScope.Response}