Jsp error when using if-else with html

I have the following on my jsp page (suppose the client is an object)

<%
 if( client == null)
 %>
NO client
 <% 
 else
%>
<a href='page.jsp?aid=<%=client.getID()%>'> and his name is  <%=client.getName()%>

thanks

+3
source share
2 answers

You are missing parentheses:

<% if( client == null) {  %>
NO client
<% } else { %>
<a href='page.jsp?aid=<%=client.getID()%>'> and his name is  <%=client.getName()%>
<% } %>

However, this is an example of bad JSP code. Consider using JSTL tags / expressions instead of scripts.

+7
source

in jstl it will be something similar to

<c:choose>
  <c:when test="${client is null}">
NO client
  </c:when>
<c:otherwise>
  <A href="<c:url value="page.jsp" >
    <c:param name="aid" value="${client.ID}" />
           </c:url>" 
  > and his name is <c:out value="${client.name}"/>
</c:otherwise>

+3
source

Source: https://habr.com/ru/post/1714371/


All Articles