Check if parameter exists in Expression Language [duplicate]

<c:if test="${param.username}" >
</c:if>

How do I check if param.username exists??


Use the not empty check.

<c:if test="${not empty param.username}" >
</c:if>

Edit: If you have a parameter of the form ?username (no value), it is safer to use ${param.username ne null}


If you want to check if parameter exists, just test if it is not null, in your case:

<c:if test="${param.username != null}"></c:if>



Broader explanation:

If you want to check:

  • if yourParam exists/ is not null:

    <c:if test="${param.yourParam != null}"></c:if>

  • if yourParam does not exist/ is null

    <c:if test="${param.yourParam == null}"></c:if>

  • if yourParam is not empty (not empty string and not null)

    <c:if test="${!empty param.yourParam}"></c:if>

  • if yourParam is empty (empty string or null)

    <c:if test="${empty param.yourParam}"></c:if>

  • if yourParam evaluates to 'true'

    <c:if test="${yourParam}"></c:if>

  • if yourParam evaluates to 'false' (string other than 'true')

    <c:if test="${!yourParam}"></c:if>


If I may add a comment...

To test if the request parameter "username" does not exist in the JSP page "a-jsp.jsp", we can write an "if" clause in the page "a-jsp.jsp":

<c:if test="${empty param['username']>
...
</c:if>

We'll go through that "if" clause if the requested URL is:

http://server/webapp/a-jsp.jsp

or

http://server/webapp/a-jsp.jsp?username=

We won't if the requested URL is:

http://server/webapp/a-jsp.jsp?username=foo