File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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

Joined: Mar 22, 2000
Posts: 13037
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.
Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
this is what i did :
URL url = new URL("http://server");
HttpURLConnection urlc =(HttpURLConnection)url.openConnection();
//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

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.

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:
subject: how to ping apache server from client side?
jQuery in Action, 3rd edition