2

I've the below JSP

<table>
        <thead>
            <th>Patient ID</th>
            <th>Name</th>
            <th>Address</th>
            <th>Contact No</th>
            <th>Disease</th>
            <th>Doctor Assigned</th>
            <th>Date</th>
            <th>Room Assigned</th>
        </thead>

        <%
            String Query = "Select ID, Name, address, contactnumber,disease,DOCASSIGN,JOINING, ROOMASSIGN from patient";
            PreparedStatement ps = conn.prepareStatement(Query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
        %>
        <tr>
            <td>
                <%
                    out.print(rs.getInt(1));
                %>
            </td>
            <td>Hi</td>
        </tr>
        <%
            }
        %>
    </table>

I'm running the query and get the data into a table td. The query is running fine the problem is if i take <td><%out.print(rs.getInt(1));%></td>, then the value is displayed in td, but when i'm trying to get it using <td><%rs.getInt(1);%></td>, the td is showing blank.

I want to know if in jsp, to display value, we have to use out.print() for sure, or i can display the value directly using <td><%rs.getInt(1);%></td>.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
user3872094
  • 3,269
  • 8
  • 33
  • 71

1 Answers1

2

There are two basic ways to display values in JSP. First, as you noted, you can execute code between <% and %>. If you use out.print or out.println there, it will be displayed on your page:

<% out.print(rs.getInt(1)); %>

Alternatively, you can have an expression between <%= and %> (note the =!). This expression will simply be printed to the page:

<%= rs.getInt(1) %>
Mureinik
  • 297,002
  • 52
  • 306
  • 350