Question

Am reading Cookies and checking if a particular Cookie is found. If particular Cookie is found, am setting value into some variable and using it for further checks. Below is the code.

<%
        String showPacksCookie = "DisabledPackagesTab";
        Cookie[] cookielistTab = request.getCookies();

        if(cookielistTab != null)
          {
            for(int i = 0; i<cookielistTab.length; i++)
              {
                  Cookie cookie = cookielistTab[i];

                if(cookie.getName().contains(showPacksCookie) && cookie.getValue().equals("true"))
                {
                    <c:set var="showPackstab" value="display" scope="request" /> 
                  //Above is line 122
                    break;
                }
              }
        }
%>

<c:if test="${showPackstab eq 'display'}">
 <b>Tab</b><img src="/tabImage.gif" align="left" />
</c:if>

This code throws below exception,

Syntax error, insert "AssignmentOperator Expression" to complete Assignment at line 122

Is it happening because am using JSTL inside Scriplet? How to get away with this problem?

Was it helpful?

Solution

scriptlets contain Java code. <c:set> is not valid Java code. What <c:set> does is

request.setAttribute("showPackstab", "display");

So this is the code you should have in your scriptlet.

But more importantly, you should not use scriptlets at all. Stick to the EL, the JSTL, and other custom tags. If the logic is too complex to be expressed using these, then it's a sign that it doesn't belong to the view (the JSP), but to the controller (a servlet or action written in Java, invoked before the JSP, which prepares the data for the JSP, and forwards to the JSP once the data is ready).

This is not the case here, since all of the above could be rewritten as:

<c:if test="${cookie.DisabledPackagesTab == 'true'}">
    <b>Tab</b><img src="/tabImage.gif" align="left" />
</c:if>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top