简体   繁体   English

如何使用Servlet显示确认消息

[英]How can I display a confirmation message using Servlets

I want to add customers to a database using JSPs, JQuery and Servlets. 我想使用JSP,JQuery和Servlet将客户添加到数据库中。 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: 到目前为止,我的jQuery代码:

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

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

});

And my servlet: 而我的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. 当数据库中不存在该客户时,我不想显示确认消息。

您可以在提交时向客户端写一个cookie,然后检查该cookie是否在那里,如果不进行正常处理,则显示消息。

Assuming that you have a jsp like login.jsp which posts to servlet eg myservlet which in turn calls a method to query to database. 假设您有一个类似login.jsp的jsp,该jsp会发布到servlet,例如myservlet,后者又会调用一种方法来查询数据库。 I would try the below :- 我会尝试以下:

  1. Post the user provided values to the servlet myservlet (post method). 将用户提供的值发布到servlet myservlet(post方法)。 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 . 2.如果是,则对Request.jsp使用RequestDispatcher。 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. 如果用户不存在,请继续常规流程并创建用户。

1)If you are using ajax for submitting your form data you can write responses and the same will be displayed, eg:- 1)如果您使用ajax提交表单数据,则可以编写响应并显示响应,例如:

//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:- 2)如果您不使用ajax,则可以在servlet的一个属性中写入响应,并在jsp中打印该属性的响应,例如:

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

and in jsp 并在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:- 但是由于不鼓励使用jsp中的scriptlet,因此可以使用JSTL标签,例如:

${requestScope.Response} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM