0

Is there anything wrong with my code? My if else statement is not working.

<%
String pages = request.getParameter("page");
%>
<input type="hidden" id="page" value="<%=pages%>"/>
        <div class="navbar navbar-fixed-bottom">
        <div class="navbar-inner">
            <a class="brand" href="index.jsp">PeachMangoPie</a>
            <ul class="nav pull-right">
            <li <%if(pages=="papoy"){out.write("class='active' ");}%>><a href="index.jsp?page=papoy">Papoy</a></li>
            <li class="divider-vertical"></li>
            <li <%if(pages=="chuckie"){out.write("class='active' ");}%>><a href="index.jsp?page=chuckie">Chuckie</a></li>
            <li class="divider-vertical"></li>
            <li <%if(pages=="dutchmill"){out.write("class='active' ");}%>><a href="index.jsp?page=dutchmill">Dutch Mill</a></li>
            <li class="divider-vertical"></li>
            <li <%if(pages=="icecream"){out.write("class='active' ");}%>><a href="index.jsp?page=icecream">Ice Cream</a></li>
            <li class="divider-vertical"></li>
            <li <%if(pages=="chicken"){out.write("class='active' ");}%>><a href="index.jsp?page=chicken">Chicken</a></li>
            <li class="divider-vertical"></li>
            <li <%if(pages=="straberrycake"){out.write("class='active' ");}%>><a href="index.jsp?page=strawberrycake">Strawberry-Cake</a></li>
            </ul>
        </div>
        </div>
Uooo
  • 6,204
  • 8
  • 36
  • 63
t0odlez
  • 25
  • 1
  • 4
  • 4
    *Is there anything wrong in my code?* yes, usage of scriptlets: [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197). – Luiggi Mendoza Jul 29 '13 at 00:48

3 Answers3

4

You need to use the .equals method to test for string equality. Try this instead:

<li <%if(pages.equals("papoy")){out.write("class='active' ");}%>><a href="index.jsp?page=papoy">Papoy</a></li>
Harshal Pandya
  • 1,622
  • 1
  • 15
  • 17
1

To fix your code: use equals to compare Strings (as shown in Harshal Pandya's answer).

To really fix your code: stop using scriptlets as explained here: How to avoid Java code in JSP files?. Rewrite all this using EL, JSTL and JavaScript. I wrote a basic example on this to omit all that hard-to-maintain scriptlet and HTML code:

<script type="text/javascript">
    window.onload = function() {
        var page = '<c:out value="${param.page}" />';
        var ulOptions = document.getElementById('ulOptions');
        var liItems = ulOptions.getElementsByTagName("li");
        for(var i = 0; i < liItems.length; i++) {
            var liItem = liItems[i];
            var a = liItem.getElementsByTagName("a")[0];
            if (a !== undefined && a.innerText.toLowerCase() === page) {
                liItem.setAttribute("class", "brand");
            }
        }
    };
</script>

<input type="hidden" id="page" value="${param.page}"/>
<div class="navbar navbar-fixed-bottom">
    <div class="navbar-inner">
        <a class="brand" href="index.jsp">PeachMangoPie</a>
        <%--
            provided id for ul HTML component in order to ease
            JavaScript onload function development
        --%>
        <ul id="ulOptions" class="nav pull-right">
            <li><a href="index.jsp?page=papoy">Papoy</a></li>
            <li class="divider-vertical"></li>
            <li><a href="index.jsp?page=chuckie">Chuckie</a></li>
            <li class="divider-vertical"></li>
            <li><a href="index.jsp?page=dutchmill">Dutch Mill</a></li>
            <li class="divider-vertical"></li>
            <li><a href="index.jsp?page=icecream">Ice Cream</a></li>
            <li class="divider-vertical"></li>
            <li><a href="index.jsp?page=chicken">Chicken</a></li>
            <li class="divider-vertical"></li>
            <li><a href="index.jsp?page=strawberrycake">Strawberry-Cake</a></li>
        </ul>
    </div>
</div>

Explanation of the provided code:

  • ${param.page} replaces request.getParameter("page");
  • window.onload = function() { ... }; defining a JavaScript function to execute after loading the page (more info: onload Event).
  • <c:out> will print text directly on the generated HTML. More info: <c:out>.
  • document.getElementById returns an HTML element by its id (the function name explains this pretty straightforward).
  • getElementsByTagName returns a NodeList that contains all the inner HTML elements with the corresponding tag name (the function name explains this pretty straightforward).
  • for(var i = 0; i < liItems.length; i++) traverse all the <li> elements inside ulOptions.
  • liItem.getElementsByTagName("a")[0] as explained before, get the <a> element in position 0.
  • a !== undefined && a.innerText.toLowerCase() === page verifying that a exists (this for those <li> without the <a> element inside) and verifying the inner text against the page request parameter.
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

This is better approach (without scriptlets):

<li <c:out value="${param.page=='papoy'?'class=active':''}"/>">
   <a href="index.jsp?page=papoy">Papoy</a>
</li>
Alex
  • 11,451
  • 6
  • 37
  • 52