• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to ping apache server from client side?

 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much, i have been able to implement the solution in Ajax very quickly.thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic