Reputation: 311
Ok, maybe this is a very common question, but I can't find a right answer...
I have a function in Java (actually, is a Servlet) and what I need/trying to do is this:
After doing some things, I need to show a "confirm" window, before doing some other things in my code... I'm doing this through javascript, of this way:
out.println("<script type='text/javascript'>");
out.println("result = confirm(Do you wanna to record in DB?)");
out.println("if(result)");
out.println(" alert('will record');");
out.println("else");
out.println(" alert('will not record')");
out.println("</script>");
out.flush();
But the lines that come after of that code, are executed before that my dialog message is displayed. So, now I want to call different functions (where I have alert). Functions inside my Java class/servlet...
So... How can I call a Java function inside my javascript code? Or what I need to do to make my code wait until I confirm my message?
Thank you
Upvotes: 0
Views: 3396
Reputation: 6881
Unfortunately, the answer to your question is basically "you can't do that", at least not the way you are envisioning. The servlet communicates with the browser over the HTTP protocol, and the browser only understands complete web pages, one per HTTP request. Even if the browser did show the alert box, your server code will have no way to know if the user clicked it, because clicking an alert will not generate an HTTP request by itself. A new HTTP request is required whenever you want to send data back to the server, including the data representing the fact that the user clicked ok.
Until the response is complete, the browser will not render the page. Each request gets one response from the servlet. You must complete the WHOLE response and send it all at once. You can use if statements in the Java code while you figure out what response to send, but anything in the javascript or html will not run until the browser has recieved your completed request. When you called flush, you only sent the latest portion of your still incomplete page (you had not sent </body></html>
and closed the connection so it's incomplete).
The net result is that you cannot ask the user part way through the response what they want, or send them messages while you produce the response (i.e. your alert).
If you want solicit user input (including button clicks) and respond to it, you must send a simple but complete page that requests the user's input, then you can do one of the following things:
The core thing to remember is that the browser and the server are separate machines and can only talk via http requests that originate from the browser. The server has no way to contact the browser except in response to a request.
Upvotes: 1
Reputation: 61
Your going to need some kind of restful call. If you want to implement a custom rest api look into this javascript object XMLHttpRequest()
or just use an html form. Point it at where your httpservlet is listening. The downside is you only get post and get. eg <form action="your server" method="get/post" />
Upvotes: 0
Reputation: 719446
First, you need to get clearly into your head where the respective code is executed ... and when.
The JSP code is executed on the server side BEFORE the HTML and its embedded Javascript is sent to the browser.
The Javascript code is executed on the browser size AFTER the browser has received the HTML and its embedded Javascript.
So what you want is for the Javascript to ask for a confirmation and THEN have the record saved to the database. So obviously, the record should not be saved by that JSP ... or at least not unconditionally. What has to happen is that the clicking of the confirmation button has to cause the Javascript to send a request to the server to say that action is confirmed. The sequence is:
There are a couple of ways of structure the confirmation interactions:
In the latter case, the request could be handled by the same handler as the original request, or by a different one.
Upvotes: 2
Reputation: 76
Usually, javascript runs on browser and Java class runs in your servlet container. You can implement your logic by javascript and data operation by java. Jquery will help you to use ajax easily.
out.println("<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.10.0.min.js\"></script>");
out.println("<script type='text/javascript'>");
out.println("result = confirm(Do you wanna to record in DB?)");
out.println("if(result) {");
out.println(" alert('will record');");
out.println("$.ajax({" +
"type: \"POST\"," +
"url: \"/your_servlet_url\"," +
"data: \"action=record\"," +
"success: function(msg) {" +
" alert( \"Data Saved: \" + msg );" +
"}" +
"});");
out.println("} else {");
out.println(" alert('will not record');");
out.println("$.ajax({" +
"type: \"POST\"," +
"url: \"/your_servlet_url\"," +
"data: \"action=norecord\"," +
"success: function(msg) {" +
" alert( \"Data Saved: \" + msg );" +
"}" +
"});");
out.println("}");
out.println("</script>");
out.flush();
Then you need to get what the parameter action value is and do what you want in your servlet.
Upvotes: 1