tags:

views:

511

answers:

1

Hi,

I would like to use c:import or c:url to grab the contents of a page (adserver). In addition, I need to specify a timeout, and if the call either times out or the page is not accessible I need some default text to be returned as my value.

Is there a jstl tag lib to do this? Or do I need to create my own?

+2  A: 

Kind of.

c:import opens a socket to the server and simply returns what the connection does (raw html in your case). If the server returns the page that is a 404 then that is what will be displayed, a 500 then you get the error page for that.

Becasue it is a socket then it has access to all the socket errors. For a timeout:

 java.net.ConnectException: Operation timed out

Unknown host:

 java.net.UnknownHostException: www.googasdasdasdassdle.com

This means that you can wrap your import in a catch statement and handle right there on the page.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:catch var="socketExceptionVariable">
    <c:import url="www.googasdasdasdassdle.com"/>
</c:catch>

<c:if test="${socketExceptionVariable != null}">
    <p>There was an error here</p>
    <c:out value="${socketExceptionVariable}"/>
</c:if>

If the import happens then it works as intended, but if something (anything) goes wrong then your error page is displayed.

You could write your own import tag but that encapsulates this but its a fair bit of work compared to this solution.

Hyposaurus
PS you might want to fix your tag jstltimeout to be two actual words.
Hyposaurus