3

Is there any way that we can use a variable in a <c:import> statement in JSP such as: <c:import url="<%=_header%>"></c:import>, where _header is a JSP String defined thus;

// host will typically equal: uk.domain.com or fr.domain.com
String host = request.getServerName();
// cc is attempting to become the country code for the domain
String cc = host.substring(0, host.indexOf("."));

String _header = "http://assets.domain.com/" + cc + "/includes/header_" + cc + ".jsp";

We host a number of sites across multiple markets. Being able to define one template this way would be ideal as it would mean fewer changes to templates. Unfortunately whenever including <c:import url="<%=_header%>"></c:import> the server fails to load the page.

But including, for instance: <c:import url="http://assets.domain.com/uk/includes/header_uk.jsp?market=<%=cc%>"></c:import> seems to work fine...

Any thoughts?!


Edit: Turns out the <%=cc%> var in the URL wasn't actually working. Had to do the following instead to get it to work;

String cc = host.substring(0, host.indexOf("."));
session.setAttribute("cc", cc);

...

<c:import url="http://assets.domain.com/uk/includes/header_uk.jsp"><c:param name="market">${cc}</c:param></c:import>

Still haven't solved the variable URL problem yet, however...

skaffman
  • 398,947
  • 96
  • 818
  • 769
Alex
  • 743
  • 7
  • 17
  • Unfortunately as we don't control the environment that we're working on, we can't narrow down the problem - any time the server detects a runtime error it redirects the browser to a 404 page. Not exactly helpful! – Alex Jul 19 '11 at 10:54

1 Answers1

3

You can't reliably mix scriptlets with taglibs/EL. They runs in different moments and scopes. You should choose to use the one or the other. Since the use of scriptlets is officially discouraged since JSP 2.0 (released Nov 2003), I'd recommend to drop it altogether and go ahead with taglibs/EL only.

The following scriptlet

<%
    // host will typically equal: uk.domain.com or fr.domain.com
    String host = request.getServerName();
    // cc is attempting to become the country code for the domain
    String cc = host.substring(0, host.indexOf("."));
%>

can be replaced by the following taglib/EL:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<c:set var="cc" value="${fn:split(pageContext.request.serverName, '.')[0]}" />

so that it's available as ${cc} in EL.

<c:import url="http://assets.domain.com/${cc}/includes/header_${cc}.jsp" />
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That's really awesome to know, thank you very much! Time to start reading up on taglibs/EL I guess! We've sort of been thrown in at the deepend with this CMS as none of us are really that confident in Java/JSP so it's been a great learning experience. If I could vote up I would... damn me for being a newbie to Stack! – Alex Jul 23 '11 at 10:46
  • @Aito: press `Ask Question` button on right top and elaborate in detail. – BalusC Aug 02 '11 at 17:19
  • @BalusC: Your solution didn't work for me. But then, after searching, I found the problem: I tried to use it inside a tag file, and it doesn't work if I don't put the 'isELIgnored="false"' flag. So, sorry, you were right. Thanks for posted it! – Aito Aug 03 '11 at 15:11
  • @Aito: You're welcome. In the future, please press `Ask Question` on right top instead of abusing comments to post questions. – BalusC Aug 03 '11 at 15:14