Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsp error when using if-else with html

Tags:

java

html

jsp

I have the following in my jsp page (assume that client is an object )

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

thanks

like image 442
vehomzzz Avatar asked Dec 23 '22 10:12

vehomzzz


2 Answers

You're missing the brackets:

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

That said, this is an example of bad JSP code. Consider using JSTL tags / expressions instead of scriptlets.

like image 200
ChssPly76 Avatar answered Dec 31 '22 11:12

ChssPly76


in jstl it would 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>

like image 38
Peter Avatar answered Dec 31 '22 10:12

Peter