views:

553

answers:

5

We are hosting a site for a client and they want us to include the header they have on their server into the pages we are hosting. So whenever they change it, it will automatically change on our site.

We are attempting to use the "include" tag in our JSP code. The code we are using is as follows:

<%@ include file="www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" %> .

We also tried

<%@ include file="http://www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" %> .

Unfortunately these aren't working for us. What seems to be happening is that the code is ONLY looking locally for this file and never seems to go "outside" to look for it.

We are able to pull the header into our page when we use an iframe but because of the way the header is constructed/coded the mouse over drop-down menus aren't working as they should when we use the iframe. The drop-down menus are "cascading" underneath the rest of the content on the page and we weren't able to bring them to the "top".

As a temporary work around, were are hosting the HTML on our own servers.

Any ideas?

+1  A: 

JSP includes don't support including remote files, which is why a relative URL is required: http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html

I suggest writing a function which opens a connection to that page and downloads the contents and then prints them to your own out stream. Then you can put that function in a local file and just include that.

Eli Courtwright
+2  A: 

If you choose to do this in Java, it's nice and easy using the HttpClient from Apache Commons.

public static String fetchSourceHtml( String urlString ) {

  try {
    HttpClient httpClient = new HttpClient();
    GetMethod getMethod = new GetMethod( urlString );
    getMethod.setFollowRedirects( true );

    int httpStatus = httpClient.executeMethod( getMethod );

    if (httpStatus >= 400) {
      return "";
    }

    String sourceHtml = getMethod.getResponseBodyAsString();
    return sourceHtml;
  }
  catch (IOException e) {
    return "";
  }
}

For a quick and dirty solution, your JSP you can call this method directly. You could, of course, create a taglib tag to call the method if you prefer.

You may want to change the time-out and retry mechanism for HttpClient. By default it will automatically try up to a maximum of 3 times with each attempt timing out after 30s.

However, you probably want to look into caching the strings for a suitable period of time. You really don't want to make 2 blocking external http requests for each page access to your site.

Cheekysoft
A: 

JSP includes are not meant to work like that with external servers. Here is a completely horrible way to fix your problem, but it was the only option for me in a similar situation. Write a class to actually parse the html from that site, and then print it out. I would add that whenever you are going to do something like this, it is always a good idea to have some sort of authentication mechanism in place.

+1  A: 

How about using the JSTL core library and doing:

<c:import url="http://www.CLIENT.com/CLIENT2/MiddlePageFiles/Vendor_header.html" />

That should be able to include remote content at request time.

Keeg
This is fine to get you up and running, but consider calling a method of your own that caches the headers and footers.
Cheekysoft
A: 

I'm not sure (I never tried), but if the header is only HTML is there not a solution in HTML such as

<div id="header" datasrc="http://CLIENT.com/..."&gt;&lt;/div&gt;

(or maybe using <iframe>)

Vinze