title: How to display data from servlet obtained using ResultSet in JSP
body: <p>I don't understand how to pass and display the values to a jsp obtained in the servlet using <code>ResultSet</code>. I saw many examples. I don't understand the <code>foreach</code> loop. Why are there two variables? Does no one use the <code>jsp:useBean</code> anymore? </p>
  
  <p>How do I pass and display the data. I tried using the bean class but the data in <code>ResultSet</code> is a table and I don't know how to set and get the values of all rows.
  Please help.</p>
  
  <p>This is the servlet:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
  
      Connection connection = null;
      Statement stmt = null;
  
  
  
      try {
          StringBuffer data = null;
          try {
              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          } catch (ClassNotFoundException ex) {
              Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
          }
          connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DesktopScreen","sa","sa123");
          stmt = connection.createStatement();
  
          String query = "select * from ClientLogin";
          ResultSet rs = stmt.executeQuery(query);
          while (rs.next()) {
              String UniqueId,ClientId,RequestedDateTime,ConnectionStatus;
              UniqueId = rs.getString("UniqueID");
              ClientId = rs.getString("ClientId");
              RequestedDateTime = rs.getString("RequestDateTime");
              ConnectionStatus = rs.getString("ConnectionStatus");
              BeanClass beanClass = new BeanClass();
              beanClass.setUniqueId(UniqueId);
              beanClass.setClientId(ClientId);
              beanClass.setConnectionStatus(ConnectionStatus);
              beanClass.setRequestDateTime(RequestedDateTime);
  
              data = new StringBuffer();
              data.append(UniqueId);
              data.append(ClientId);
              data.append(RequestedDateTime);
              data.append(ConnectionStatus);
  
  
          }
          request.setAttribute("data", data);
          request.getRequestDispatcher("Display.jsp").forward(request, response);
      }
      catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      finally
      {
  
          out.println("&lt;/center&gt;");
          out.print("&lt;/table&gt;");
          out.println("&lt;/body&gt;");
          out.println("&lt;/html&gt;");
          out.close();
          try {
              connection.close();
          } catch (SQLException ex) {
              Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  
  
  
  }
  </code></pre>
  
  <p>}</p>