1

I am trying to create an HTML Report that will print out the different line items. If the item is null, then I want it to skip the item, but I want to code to be able to handle the item when it is not null. My code currently is giving me an illegal expression error on my if statement. I think I have some kind of variable type issue.

<tr><td>
<%= if(rs.getString("delivery0delivery_address_2") != null){

      rs.getString("delivery0delivery_address_2")

     }
 else{

     %>
     <br>
     <%=
      }
      %>
MLGinger96
  • 23
  • 2
  • What tools and/or frameworks are you using? My point being that writing tests about the resultset in your jsp isn't optimal at all. So in order to have a cleaner easy-to-maintain code, you should leave your tests about your resultset in the backend of your software, and only deal with presentation on your JSP. – DamCx Jun 28 '18 at 13:27
  • I am working in Netbeans accessing a SQLServer 2017 database. I need to have the information I pull from the database overlay an 8x14 Invoice template that is already printed on a piece of paper. – MLGinger96 Jun 28 '18 at 13:40

2 Answers2

0

First point worth noting is that <%= ... %> is not same as <% ... %>.

You cannot use JSP expressions as you're using now.

Either drop the = before the if clause and use simple JSP scriplet tag, otherwise if you're using JSP expressions, then use it like this:

<tr><td>
<%= (rs.getString("delivery0delivery_address_2") != null) ? rs.getString("delivery0delivery_address_2") : "<br>"%>

You can visit JSP - what's the difference between “<% … %>” VS “<%= … %>” to clearly understand the difference between both JSP tags.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • I see, my professor who taught me Java always did the <%= instead of <% and never really explained the difference. It's good to know the differences, I still don't think that fixes my main problem, but it is definitely good to know. – MLGinger96 Jun 28 '18 at 13:43
  • @MLGinger96 you cannot use `if` statement within `<%= ... %>` tags. That's just not valid and your program won't compile. Either change the tags or change the `if` statement. – Raman Sahasi Jun 28 '18 at 13:46
  • I see, that fixes it. Thank you very much for the help and the insight. – MLGinger96 Jun 28 '18 at 13:53
0
<%= (rs.getString("delivery0delivery_address_2") != null) ? rs.getString("delivery0delivery_address_2") : "<br>" %>

Or if you want to use the complete if statement

<% if(rs.getString("delivery0delivery_address_2") != null){ %>

      <%=rs.getString("delivery0delivery_address_2")%>

    <% }else{ %>
     <br>
    <% } %>

Let me tell you something, using scriplets is really discouraged in java web development. Use custom tag libraries for showing java variables/lists on the front end.

And you should not have the result set on your front end code. Separate the business logic from the presentation view.

Farhan Qasim
  • 990
  • 5
  • 18