Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:38:47+00:00 2026-06-09T17:38:47+00:00

I need some help with forwarding resultset(rs) to a jsp . I’ve implemented the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:38:48+00:00Added an answer on June 9, 2026 at 5:38 pm

    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?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help with this problem in implementing with XSLT, I had already implemented
Need some help to solve this. I have a gridview and inside the gridview
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help with crypto routines in Java. Given a PKCS#7 signature, I want
I need some help with operators new and delete I tried to create a
Need some help with this error. Fresh wordpress 2.9 install... Fatal error: Cannot instantiate
Need some help on understanding how to do this; I'm going to be running
Need some help please with this error : TypeError : 'unicode' object does not
need some help figuring this out. My function: function cleanDatabase() { $allowedTime = $this->time
Need some help refactoring this if/else block that builds the conditions for a find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.