2

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 :

  1. basic form : Where user enters his choice .
  2. On submission the flow get directed to a servlet.
  3. From servlet the flow goes to a Java file where the data base retrieval and other logic is taken care of.
  4. Then the result is send back to the servlet .
  5. Servlet forwards the result to a JSP for display.

Servlet :

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); 
          }
        }

The java file :

    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<Types> getTypes(String test) {

          ResultSet rs = null;
         List<Types> 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; 
          }
     }

As suggested in solution below , Another model class

    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;   
             }  
    } 

The final JSP display file

<%@ page import="java.util.*" %>
<%@ page import="com.example.model.Types" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<html>
<body>
<h1 align="center">Final Data JSP View</h1>
<p>

<%

List<Types> styles = (List<Types>) request.getAttribute("styles");
if(styles!=null){
    for(Types type: styles){  
        out.println("<br/>" + type.getName() + " " + type.getPCANo()+ " " + type.getIP());  
        }  
    } 
%>
            </body>
</html>

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.

ABC PCA100 XXX.1.0.0
ABC PCA100 XXX.1.0.0
ABC PCA100 XXX.1.0.0

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
misguided
  • 3,699
  • 21
  • 54
  • 96

1 Answers1

4

You have to create a model class which represent Name, PCANo and IP.

public class Types
{
  private String name;
  private String pcaNo;
  private String ip;
  //constructors
  //getter-setters
}

and getTypes method returns List<Types> of CoffeeExpert class.

 public List<Types> getTypes(String type) {
     Connection con = getConnection();
     String Query = "select * from ABC";
     List<Types> list=new ArrayList();
     rs = con.createStatement().executeQuery(Query);

      while (rs.next()) {
         Types type=new Types();
         type.setName(rs.getString(1));
         type.setPcaNo(rs.getString(2));
         type.setIp(rs.getString(3));
         list.add(type);
      }
      rs.close();
      con.close();
    return list;
  }

To show the List<Types> in .jsp page:

JSP Tags:

<%
  List<Types> styles = (List<Types>) request.getAttribute("styles");
  if(styles!=null){
    for(Types type: styles){
       out.println("<br/>" + type.getName() + " " + type.getPcaNo());
     }
  }
%>

JSTL:

<c:forEach var="type" items="${styles}">
  <br/>
  <c:out value="${type.name}" />
  <c:out value="${type.pcano}" />
  <c:out value="${type.ip}" />
</c:forEach>

Reference SO threads:

  1. JSTL FAQ - Use JSTL 1.2 it requires single http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar jar file.
  2. How to avoid Java Code in JSP-Files?
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Hi AVD , When I try to put the JSP tag inside my jsp i'm getting the following error in eclipse "Types cannot be resolved to a type". Also I'm not familar with how to add JSTL tags in JSP :( . Can you kindly advise. – misguided Aug 16 '12 at 03:56
  • 1
    You have to create `Types` class and import package or use fully qualified path while using `Types` class - `com.example.model.Types` – KV Prajapati Aug 16 '12 at 04:07
  • Thanks mate , that was very helpful . I'm still not sure about the jstl tags though, as in where to put them in JSP . Found from the internet that I need to import jstl.jat and standard.jar file into my web-inf lib folder to use JSTL `<%@ page import="java.util.*" %> <%@ page import="com.example.model.Types" %>

    Final Data JSP View

    <%List styles = (List) request.getAttribute("styles"); if(styles!=null){for(Types type: styles){ out.println("
    " + type.getName() + " " + type.getPCANo()+ " " + type.getIP()); }} %> `

    – misguided Aug 16 '12 at 04:23
  • 1
    @I've added useful links about JSTL in my post. – KV Prajapati Aug 16 '12 at 04:41