I’m not sure if I can do this, but I’m trying to print a string with Expression Language only (no JSTL). The string is going to often-times be null, and I want an empty string printed to HTML instead of the word “null”. This is what I’m trying (without success):
<%
myString = "";
myString = someMethodThatMayReturnNull(); // returns string or null
%>
<b>${myString}</b><br />
<b>${ (empty myString) ? "myString-is-empty" : "myString-is-NOT-empty" }</b><br />
The reason I don’t want to use JSTL is because I’m trying to make the minimum changes to a fairly good sized system.
Scriptlets and EL doesn’t share the same variable scope. You need to prepare EL variables as attributes of page, request, session or application scope. As you have right now, it is always empty.
Note that you normally do that Scriptlet part inside a Servlet. Also note that EL is null-safe, so anything which is truly
null, i.e.if (object == null)passes, then it simply won’t be printed at all.Or, if your concrete problem is that it prints literally
${myString}and so on in the browser, while it worked with JSTL<c:out>, then it means that you aren’t using a JSP 2.0 compatible servletcontainer or that your webapp’sweb.xmlis not declared conform at least Servlet 2.4, or that you haveisElIgnoredset totrue. Verify and fix it all.