0

I have some code that I am using to grab the page name, and I want to compare the variable to a string - but the statement always returns "true".

<%
    String uri = request.getRequestURI();
    String pageName = uri.substring(uri.lastIndexOf("/")+1);
%>

<c:if test="${pageName != 'home.html'}" >
<script src="<%= designPath %>/javascripts/constants.js" type="text/javascript"></script>
<script src="<%= designPath %>/javascripts/application.js" type="text/javascript"></script>
<script src="<%= designPath %>/javascripts/cart.js" type="text/javascript"></script>
</c:if>
user664833
  • 18,397
  • 19
  • 91
  • 140
m_gunns
  • 551
  • 1
  • 16
  • 29

4 Answers4

4

Should try:

<c:if test="${pageName ne 'home.html'}" >
Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
2

If you're already using pure Java code to get the uri and page name, you might as well use pure java code for the if-statement too. Since you're already breaking the sacrosanct "no Java code in a JSP" rule.

 <% 
 String uri = request.getRequestURI();
 String pageName = uri.substring(uri.lastIndexOf("/")+1);
 if( !"home.html".equals(pageName) )
 {
    out.print("<script src='" + designPath + "/javascripts/constants.js' type='text/javascript'></script>");
    out.print("<script src='" + designPath + "/javascripts/application.js' type='text/javascript'></script>");
    out.print("<script src='" + designPath + "/javascripts/cart.js' type='text/javascript'></script>");
 }
 %>
developerwjk
  • 8,619
  • 2
  • 17
  • 33
0

Contary to the comment, this does work (Tomcat 6.0.20) and jstl.jar:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
pageContext.setAttribute("pagename", "test");
%>
pagename = <c:out value="${pagename}" />
<br>
<c:if test="${pagename == 'test'}">
equal
</c:if>

equal will be printed.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
0

Either != or ne are valid comparison operators in JSTL. Just be sure of importing the tag library with prefix c. And for sanity you should echo the pageName value. Just to be sure that it is different from 'home.html'.

luiso1979
  • 868
  • 1
  • 6
  • 18