0

I need to create a special condition if the page URL in the browser address bar ends in "search". In my .jpsf file, I have this code so far:

<c:choose>
    <c:when test="${SOMETHINGHERE eq 'Search'}">
        <c:out value="Search" />
    </c:when>
    <c:otherwise>
        <c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" />
    </c:otherwise>
</c:choose>

What can I put in the SOMETHINGHERE section to detect if the URL contains the word search? Browsing the posts here and searching on Google has not led me to any answers that work so far.

I also tried adding <% String currentPDP = request.getParameter("search")%> but it returned a 500 error, probably because _Search is not set up properly as a parameter (it's hard coded at the end of a URL) and the code is not properly formatted.

I am new to JSP and have to learn on the job, so I appreciate your patience and any help you can offer. Thank you kindly.

MrLore
  • 3,759
  • 2
  • 28
  • 36
surfbird0713
  • 1,209
  • 2
  • 23
  • 45

1 Answers1

0

First you'd want to import the Functions tag with:

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

Then, you can use:

${fn:contains(pageContext.request.requestURI, 'Search')}

or, if you don't care about the case

${fn:containsIgnoreCase(pageContext.request.requestURI, 'Search')}

That will allow you to search through the URI for the term Search.

If the full url to the running script was http://www.example.com/site/search.jsp, then responseURL should return /site/search.jsp. The above examples would return true in the second case because 'Search' is in the URI string (the first would fail due to capitalization).

It does not include query parameters, which would involve a different check.

jcern
  • 7,798
  • 4
  • 39
  • 47
  • Unfortunately this isn't doing anything...not sure why. I placed the import Functions snippet at the top of the JSP file and then put: ${fn:containsIgnoreCase(pageContext.request.requestURI, 'Search')} into the test = but it doesn't work. – surfbird0713 Oct 03 '12 at 20:55
  • Yesterday I was trying to accomplish another task with requestURI and it was only pulling in the name of the .JSP file, not the actual URL in the browser. Maybe that's why it's not working? – surfbird0713 Oct 03 '12 at 20:56
  • if you output does the URL appear? – jcern Oct 03 '12 at 21:02
  • I double checked the code above, so I am not sure what is happening. You may also want to output the results of ${fn:contains('Search', 'Search')} which should return true, just to see what is not working. – jcern Oct 03 '12 at 21:28
  • Finally, if none of the above work you may want to try adding this directive to the page: <%@ page isELIgnored ="false" %>. It will force EL to be enabled, I would think it already is - but just throwing this out as one last idea about a possible cause. – jcern Oct 03 '12 at 21:29
  • with the c:out statement, I was able to get results, but causes the name of the .jsp file to appear:/directory/directory1/directory2/ProductDisplayShell.jsp, rather than /directory/directory1/directory2/this-is-my-product-page_search which is what I'm looking for. It is just reading the JSP file name that is generating the page rather than the URL the user is seeing. Is that because I'm working with a local host on a developer toolKit rather than our development/qa/staging site? – surfbird0713 Oct 04 '12 at 14:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17561/discussion-between-surfbird0713-and-jcern) – surfbird0713 Oct 04 '12 at 14:52