wood burning stoves 2.0*
The moose likes JSP and the fly likes how to ping apache server from client side? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to ping apache server from client side?" Watch "how to ping apache server from client side?" New topic
Author

how to ping apache server from client side?

Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
i have a JSP application. i want the client to recieve a notification in case its connection with the server breaks. how can i do it?

one possibility is to open a server socket on the server and create a socket at the client and keep checking regularly after some time (using javascript) if the socket is still connected.

but is there any way i can contact and check if i can access apache server, so that i dont have to write an extra piece of code.when the connection will break i will not be able to connect to the apache and hence the client can be notified.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Oh no! DO NOT try to mix sockets and servlets unless you realllllly know what you are doing.
Your client can use Java (or an applet or Javascript) to make a HttpURLConnection to the server to get the status. You will just have to define a servlet that will respond with a status message.
Bill
Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
this is what i did :
try
{
URL url = new URL("http://server");
HttpURLConnection urlc =(HttpURLConnection)url.openConnection();
urlc.connect();
urlc.getResponseCode();
}
catch(java.io.IOException)
{
//print it
}

now this code is embedded in a javascript function which pings itself every second. when server is connected the response code is 200. when i remove the LAN wire, then also there is no IOException. worst it still gives the 200 code. what do i do? i am going crazy. from where does my client access the server???
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The above code is Java. If it's "embedded in a JavaScript function" inside a JSP or Servlet all that's really getting "embedded in a JavaScript function" are the results of that code when it runs on the server to generate the webpage. In other words - This code is not being run each time inside your JavaScript function on the client - it is simply printing out the string ("200") that was placed there when this code ran on the server.

William Brogden provided the solution that you need above - you're going to need to use an Applet or JavaScript to do this. JavaScript is probably the easier solution - and AJAX (which is using JavaScript to make a HTTP request in the background) is a pretty hot up-and-coming topic right now. Do a search in our "HTML and JavaScript" forum (or Google) for "AJAX" - it's probably just what you're looking for.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
thank you so much, i have been able to implement the solution in Ajax very quickly.thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to ping apache server from client side?