title: Resultset in JSP
body: <p>I need some help with forwarding resultset(rs) to a jsp . I've implemented the MVC structure in JAVA (note: I'm new in this). The logic flow for the same is below :</p>
  
  <ol>
  <li>basic form : Where user enters his choice .</li>
  <li>On submission the flow get directed to a servlet.</li>
  <li>From servlet the flow goes to a Java file where the data base retrieval and other logic is taken care of.</li>
  <li>Then the result is send back to the servlet .</li>
  <li>Servlet forwards the result to a JSP for display.</li>
  </ol>
  
  <p>Servlet : </p>
  
  <pre><code>package com.example.web;
  import com.example.model.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.*;
  import java.util.*;
  public class CoffeeSelect extends HttpServlet {
  
    public void doPost( HttpServletRequest request, 
                        HttpServletResponse response) 
                        throws IOException, ServletException {
              String c = request.getParameter("type");
              CoffeeExpert ce = new CoffeeExpert();
              List result = ce.getTypes(c);
              request.setAttribute("styles", result);
              RequestDispatcher view = request.getRequestDispatcher("result.jsp");
              view.forward(request, response); 
            }
          }
  </code></pre>
  
  <p>The java file : </p>
  
  <pre><code>    package com.example.model;
  
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.*;
  
      public class CoffeeExpert {
         public List&lt;Types&gt; getTypes(String test) {
  
            ResultSet rs = null;
           List&lt;Types&gt; list = new ArrayList();
           String Name = "na";
           String PCANo = "NotFound";
           String IP = "NotFound";
           Types type=new Types(); 
           if (test.equals("ABC")) {
           try{
           Connection con = getConnection();
           String Query = "select * from Table1";
           // System.out.println(Query);
  
            rs = con.createStatement().executeQuery(Query);
  
                   while (rs.next()) {
                       type.setName(rs.getString(1));
                       type.setPCANo(rs.getString(2));
                       type.setIP(rs.getString(3));
                     System.out.println(Name+"  "+PCANo+"  "+IP);
                     list.add(type);
                     }
                     rs.close();
                     con.close();
  
                 }catch (SQLException e) {
                  System.out.println("SQLException");
                  e.printStackTrace();
               }
           }
           else {
              System.out.println("Didn't find any data");
           }
           return(list);
         }
  
         public static Connection getConnection() {
  
               Connection con = null;
               String Res = "na";
               String BusinessUnit = "NotFound";
               ResultSet rs = null;
               try {
                   // Load the JDBC driver
                  String driverName = "oracle.jdbc.driver.OracleDriver";
  
                  // String driverName = "oracle.jdbc.OracleDriver";
                   Class.forName(driverName);
                   // Create a connection to the database
                   //Dev
                   String serverName = "xx.xx.xx.xx";
                   String portNumber = "1521";
                   String sid = "SSSS";
                   String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
                   String username = "SSSSS";
                   String password = "password";
                   con = DriverManager.getConnection(url, username, password);
                    return con;
                    } catch (ClassNotFoundException e) {
                  System.out.println("ClassNotFoundException");
                  e.printStackTrace();
               } catch (SQLException e) {
                  e.printStackTrace();
              }
              return con; 
            }
       }
  </code></pre>
  
  <p>As suggested in solution below , Another model class</p>
  
  <pre><code>    package com.example.model;
  
      public class Types {
          private String Name;  
          private String PCANo;
          private String IP; 
          //constructors   //getter-setters 
          public String setName(String Name){     
              return this.Name = Name;  
           }   
           public String getName() { 
              return this.Name; 
           }
           public String setPCANo(String PCANo) { 
              return this.PCANo = PCANo;  
           }  
           public String getPCANo() {  
              return this.PCANo;   
           }  
           public String setIP(String IP) { 
                  return this.IP = IP;  
               }  
               public String getIP() {  
                  return this.IP;   
               }  
      } 
  </code></pre>
  
  <p>The final JSP display file</p>
  
  <pre><code>&lt;%@ page import="java.util.*" %&gt;
  &lt;%@ page import="com.example.model.Types" %&gt; 
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; 
  
  &lt;html&gt;
  &lt;body&gt;
  &lt;h1 align="center"&gt;Final Data JSP View&lt;/h1&gt;
  &lt;p&gt;
  
  &lt;%
  
  List&lt;Types&gt; styles = (List&lt;Types&gt;) request.getAttribute("styles");
  if(styles!=null){
      for(Types type: styles){  
          out.println("&lt;br/&gt;" + type.getName() + " " + type.getPCANo()+ " " + type.getIP());  
          }  
      } 
  %&gt;
              &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The results is fetching only the last line for all the no of rows getting displayed i.e, the database table has three rows, the last row is getting displayed 3 times.</p>
  
  <blockquote>
    <p>ABC  PCA100 XXX.1.0.0<br>
    ABC  PCA100 XXX.1.0.0<br>
    ABC  PCA100 XXX.1.0.0</p>
  </blockquote>