1

I have the following code:

<%@ page language="java" session="true" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<% 
request.setAttribute("action", request.getParameter("action"));
%>

<c:choose>
    <c:when test="${action == null}">
        NULL
    </c:when>
    <c:when test="${action == view}">
        VIEW
    </c:when>
</c:choose>

However when I pass URL with ?action=view, it doesn't show VIEW.

Where am I going wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
treasureireland
  • 87
  • 2
  • 3
  • 10

1 Answers1

2

The EL expression ${view} is looking for an attribute on exactly that name in page, request, session or application scope like as how ${action} works. However, you intented to compare it to a string. You should then use quotes to make it a true string variable like so ${'view'}. It also works that way in normal Java code.

<c:choose>
    <c:when test="${action == null}">
        NULL
    </c:when>
    <c:when test="${action == 'view'}">
        VIEW
    </c:when>
</c:choose>

By the way, copying a request parameter as a request attribute using that scriptlet is clumsy. You should not be using scriptlets at all. HTTP request parameters are in EL already available by the ${param} map.

<c:choose>
    <c:when test="${param.action == null}">
        NULL
    </c:when>
    <c:when test="${param.action == 'view'}">
        VIEW
    </c:when>
</c:choose>

This way you can get rid of the whole <% .. %> line.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555