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!

Recommended Answers

All 6 Replies

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.

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

You are talking about a regular variable right? Or are you talking about an evirionment variable?

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.

i mean like a get (from url) or posted (client connection) variable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.