How to find if a url is active

hi friends
I have a list of links in my database which consists of the IP address and the service name. When the user wants to search by the name, I want to see if the link is active(i.e the server and the page is active) so that I can display only them and also update them in database such as valid or not valid. Is there any simple way of doing this?

I have found this function here:

function url_exists($url) {
    if(!strstr($url, "http://")) {
        $url = "http://".$url;

    $fp = @fsockopen($url, 80);

    if($fp === false) {
        return false;   
    return true;

if(url_exists('')) {
  echo 'Good URL';
} else {
  echo 'Bad URL';

The function Jeremy Ross just provided only indicates if the server exists, not if the page exists.

I wrote this a long long time ago:

function checkUrl($url) {
   $uri = parse_url( $url );
   $port = isset($uri['port']) ? $uri['port'] : 80;
   if ($uri['path'] == '') $uri['path'] = '/';
   $sock = @fsockopen( $uri['host'], $port, $reply, $replyString, 5);
   if (!$sock) {
     // cannot connect
     return false;
   fputs($sock, 'GET '.$uri['path'].($uri['query']?'?'.$uri['query']:'').' HTTP/1.1'."\\r\
".'Host: '.$uri['host']."\\r\
   while (!feof($sock)) {
      $line = fgets($sock, 1024);
      if ($line == "\\r\
") break;
      $replyString .= $line;
   preg_match("|^HTTP/\\S+ (\\d+) |i", $replyString, $status);
   if ($status[1] == '200') {
      // URI exists
      return true;
   } else if (substr($status[1], 0, 1) == '3') {
      // got redirected. let's just assume we're being redirected to a correct url
      return true;
   } else {
      // if the status is not 200 or 3xx it must be some error code like 4xx or 5xx
      return false;

hi ross,
thank you very much. Is there any way we can do it in servlets or jsp instead of php?