Search code examples
javajspscriptlet

JSP Printing hostname


I have below code in my JSP for non prod environments to display host name

<c:if test="${pageContext.request.serverName ne 'demo.com' and pageContext.request.serverName ne 'sdemo.com'}">
    <%
        InetAddress ia = InetAddress.getLocalHost();
        String node = ia.getHostName();
        out.println("<!-- hostname  " + node+  " -->");
    %>
    <div id="notProd"><spring:message code="nonproduction.environment.message" /> <c:out value="${node}" />
</c:if>

The above code displays the hostname correctly in hidden message when I do view source but its not displaying the node value on the page, how do I tweak the c:out to display node value on JSP?


Solution

  • <c:if test="${pageContext.request.serverName ne 'demo.com' and pageContext.request.serverName ne 'sdemo.com'}">
        <%
            InetAddress ia = InetAddress.getLocalHost();
            String node = ia.getHostName();
            out.println("<!-- hostname  " + node+  " -->");
            pageContext.setAttribute("node", node);
        %>
        <div id="notProd"><spring:message code="nonproduction.environment.message" />
     <c:out value="${node}" />
    </c:if>