JSON HEAD servlet 1.2


    It is a Java servlet lets you execute HTTP HEAD requests against an arbitrary URL and return data in JSON format. So your JavaScript application will be able to check out status code, content length, content type and modification date for the remote files (sites).

The idea is very simple. Servlets returns JavaScript code. So adding this JavaScript code dynamically to your page lets you bypass any sandbox restrictions and deal with the remote data sources.

How to use it:

a) copy headPackage.jar into your WEB-INF/lib directory.

b) define a servlet in your web.xml file.
 

    <servlet>
     <servlet-name>JsonHead</servlet-name>
     <servlet-class>com.jsos.http.JsonHead</servlet-class>
    </servlet>

define a mapping:
 

    <servlet-mapping>
     <servlet-name>JsonHead</servlet-name>
     <url-pattern>/servlet/JsonHead</url-pattern>
    </servlet-mapping>

Servlet accepts two parameters:

url - URL to be requested
callback - a name for your JavaScript callback

    The following example illustrates the usage. This code requests info about the following file http://servletsuite.com/servlets/sos.zip. JavaScript function f describes our callback.
 


<script language="JavaScript">

function f(info)
{
  var s="Response:" + info.responseCode+"\n"
   +"Size:" + info.contentLength+"\n"
   +"Type:" + info.contentType+"\n"
   +"Date:" + info.lastModified;

  alert(s);
}

var e = document.createElement("script");
e.src = 'http://your_server/servlet/JsonHead?url=http://servletsuite.com/servlets/sos.zip&callback=f';
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);

</script>

You can use this servlet as a proxy for checking external web sites availability. For example the following code performs Ajax call for testing site:
 


<script language="JavaScript">

function f(info)
{
if (info.lastModified!='')
  alert("site exists");
}

var e = document.createElement("script");
e.src = 'http://your_server/servlet/JsonHead?url=http://url_for_testing&callback=f';
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);

</script>

    For downloading:  headPackage.jar   
 

  © Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

Also in JSOS: