With server-side includes, you put <!--#include file="header.html"--> in your .shtml file, and the server inserts that file's contents before sending the page to the browser. The included file is local to the server; this directive does not fetch a URL on another machine.
This limitation becomes a real issue when you are doing ad serving. The basic approach to web ads right now is to put an <img src> tag that points to an ad server. The ad server picks an ad and returns an image; a surrounding link can route clicks through a tracking URL. That works for simple banner ads that are just images. But what if the ad is more than an image? What if it is a block of HTML with text, images, links, a form, or a Java applet? You cannot do that with an img tag. You need to insert a whole chunk of HTML into the page.
That is what include_url.java does. It fetches the contents of a remote URL and inserts them into the page. The remote URL can return any HTML, and that HTML gets included in the page as if it were local content.
The program opens the remote URL at page-serve time and reads the response into the page. It does not maintain a local copy. If the ad server must receive a request for every page view, the surrounding page and HTTP responses also need suitable cache controls. That lets the ad server rotate ads, target by time of day, and count impressions without changes to the publishing template.
Say you run a news site and you have an ad server on a separate machine. In your page template, you place a call to include_url with the ad server's URL. When a reader loads the page, include_url fetches the ad block from the remote server and inserts it. The ad block can be a banner image, or it can be a complete HTML fragment with multiple images, styled text, and JavaScript. The news site's template does not need to know or care what the ad looks like.
The ASP integration uses IIS: register the Java class as a Server Object and call it from your .ASP pages. The fetching code can also be called from other server-side Java setups, with the appropriate integration.
I wrote this because we needed something more flexible than img tags for the advertising on our sites at Philadelphia Newspapers. The advertising team wanted richer ad units, and the simple image-tag approach was too limited. This solution lets the ad server deliver whatever HTML it wants, and the publishing templates stay clean.
The code opens the URL and reads the response. If that fails, it returns an error message for the calling page to handle. This version joins the lines without restoring their line breaks, so a fragment that depends on those breaks, such as JavaScript with line comments, needs different handling. It also leaves links and image addresses unchanged; use absolute URLs or addresses that make sense in the containing page.
You can download include_url.java. The complete source is also included below.
Here is the full source code:
/**
include_url.java
Designed and Implemented by
@author Rajiv Pant (Betul) [email protected] https://rajiv.org
@version 1.0 1997/Mar/11
Description:
Since Server Parsed HTML (.shtml, .stm) or Server Side Includes (SSI)
does not provide a mechanism to "include" a remote url (i.e. on another
server), this object can be used to do just that.
This Java Object will work with any web server that can run
server-side Java (even if via CGI or shtml exec).
This component was created to demonstrate how to use Java to
create Active Server Objects that can be called from asp pages.
The advantage of using Java via asp is that most of the code
becomes easily portable to other environments. So if you move
from the ActiveX server to another server environment, you won't
have to re-create your core server applications.
Practical applications of this:
* One of its uses is to get a block of HTML from a remote ad server
that includes a link to the advertiser, the ad image, some
accompanying text and even scripts and applets.
The commonly used method of using an
<img src="http://ad-server/cgi-bin/getad?name"> can only include
an image, but not the link, text, or some code around it.
Combined with a cluster of ad servers, this makes it possible to
rotate ads of any type -- image, multimedia, html, script, applet,
anything.
* To include the output of a CGI from another server of yours.
For example, you wrote this CGI on your Unix server that you
can't port to your new NT server just yet, so you still run it
off your unix server but not directly, you "include" it in some
page in your NT server which does other things too. It could be
your advertisement rotation cgi or something.
* This object can easily be extended to behave like a proxy server
so that remotely included pages can be parsed and modified before
displaying so that the links to local urls, images and other objects
on the pages are converted to remote ones so that the links work
and the images show up. As it is, this object does not do that.
I may do that in some program I use to illustrate html-parsing using
Java.
Usage:
To use this particular example you need:
* Windows NT Server 4.0 running
Internet Information Server 3.0 with Active Server Pages.
* Microsoft Java VM (Virtual Machine) for Windows version
build 1257 or later.
* javareg.exe which comes with the freely available MS SDK for Java
or with MS Visual J++.
* The accompanying include_url.asp page that demonstrates how
to use this in your own page.
To install it for use on your server, follow these steps:
* Compile this include_url.java program to include_url.class
java bytecode. You can do this using MS Visual J++, Symantec
Visual Cafe, some other Java compiler, or javac from
Sun's JDK (Java Development Kit) by running
"javac include_url.java". Then you may want to test it by running
it as a stand-alone java program. You can do this from your
Java Development Environment like J++ or Cafe or simply by
entering "include_url.class" at the CMD prompt provided your
.class files are associated with the MS Java VM as they should be.
* Place the compiled Java class include_url.class in your
\winnt\java\trustlib folder. You can use the
"copy include_url.class \winnt\java\trustlib\" command at
the CMD prompt to do this or you can drag and drop.
* Register this object as an Active Server Object using the
javareg.exe command. You can enter:
"javareg /register /class:include_url /progid:include_url"
* Stop and start IIS. You can use
net stop w3svc & net stop msftpsvc & net start msftpsvc & net start w3svc
(If you are running the gopher service, you need to restart that in
the above command too since it is part of INETINFO.EXE.)
* Try out this object using the sample test-include_url.asp
ASP page.
Tip: Before compiling the final version of any program for
installation, remember to turn debugging off and set optimizations
in your development environment such as Visual J++ or Cafe.
*/
import java.net.* ;
import java.io.* ;
class include_url
{
// final static boolean DEBUG = true ;
final static boolean DEBUG = false ;
String display(String url_to_include)
{
String output = "" ;
try
{
URL u = new URL (url_to_include) ;
if (DEBUG == true)
{
output = output + u.toString() ;
}
DataInputStream d = new DataInputStream ( u.openStream() ) ;
String l ;
while ( ( l = d.readLine () ) != null )
{
output = output + l ;
}
}
catch (Exception e)
{
output = "Betul's include_url.java reported error: " + e.toString() ;
}
return output ;
} // end of method display()
// The main function is provided so that you can test this
// object from the command line by specifying a complete
// URL as the first argument.
// You may want to not have the main function in the final,
// to-be-installed version.
public static void main (String[] args)
{
include_url iu = new include_url() ;
String d = iu.display (args [0]) ;
System.out.println ( d );
}
} // end of class include_url