Expression Language is not working in the JSP Pages. We are just using JSP Expression Tag but it can throw Exception when the expression result becomes null. Ex: <%= h.getHtml() %> . Instead we can use ${h.html}
1 Answers
The JSP code generated in ToolTwist widgets you write is plain standard JSP code. While it is possible to use Java Beans, EL, tag libraries and various other features of JSP, most projects find that sticking with the most basic JSP expressions simplifies future maintainability, because the meaning of the code is clearest.
Most tags are designed to strip down the JSP code and make it more HTML-like, at the expense of using special semantics or moving the logic elsewhere. This separation is useful when the JSP will be maintained by a web designer who is not familiar with the application logic, but in the case of ToolTwist that objective is more or less redundant, because pages are changed using the Designer, not by editing JSP code.
Most widgets are written once, and then infrequently changed, so the important consideration is making the code easy to understand at a glance, without needing to think about or investigate any special semantics or tag library definitions. Using the simplest JSP syntax makes debugging and testing easier, and will be appreciated by any programmer who needs to look at the code in the future.
Note that in this context simplest means "easiest to understand" rather than "fewest lines of code."
Regarding your exception, remember that the code within <%= and %> is standard Java code. If the Java code throws an exception, then the JSP code throws an exception. In this case, getHtml() is a method written by you, so if you wish to use it as a string you should probably ensure it does not return null. Alternatively, check it's value before using it:
<%
String html = h.getHtml();
if (html == null) {
html = "";
}
%><%=html%>
Note that a common bad habit is to call the method twice. Best not to do this:
<%= (h.getHtml() == null) ? "" : h.getHtml() %>
Overall, I'd recommend that you change your getHtml() method to return "" where it currently returns null.

- 1,465
- 1
- 14
- 21