0

I am in the process of learning JSP and my question is how do I check to see if a variable exists? The equivalent of this in php would be

if (isset($_POST['someVar']))

or

if (isset($_GET['someVar']))

any and all help would be appreciated!

3
Contributors
6
Replies
8
Views
12 Years
Discussion Span
Last Post by Comatose
0

I'm pretty sure you can use defined in javascript. You can try:

if (defined(somevariable)) {
   alert(somevariable);
}

I'm not sure, but I think that's accurate.

0

that wont work because i need it to be serverside, otherwise i might as well do it in html/javascript instead of jsp

0

You mean a request or session attribute?
It's easy.

<c:if test="${not empty someVar}">
  <%-- do what you need to do when the attribute is there --%>
</c:if>

Actually if it's just to prevent errors when outputting something you don't need to check, JSTL is smart enough to ignore anything that's not there so

<c:out value="${someVar}"/>

would just output nothing at all if someVar weren't there.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.