Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top