简体   繁体   中英

Displaying the result set values from servlet to jsp

I need help in forwarding the result set values from servlet to jsp without using JSTL implementation

Work flow :

  1. The user enters a value in text box and clicks search button
  2. On clicking search the servlet is called. The servlet focuses on the database implementation and forward the result set values to the same jsp page from where the request comes.

Issue: My result set size is 3, but the value which is in the top of my table alone is getting printed in my jsp page. The remaining 2 values are missing.I want all the values to be printed in my jsp page.

This is my code:

Productlist.jsp

<%@page import="java.util.List"%>
<%@page import="web.Products"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>Products</title>
</head>

<body>
 <form method="post" align="center" action="ProductList">

Company Name:<input type="text" size="20" id="company" name="company" />
<input type="submit" value="search"/>
  <%
  List<Products> pdts = (List<Products>) request.getAttribute("list");
  if(pdts!=null){
    for(Products prod: pdts){
       out.println("<br/>" + prod.getProductname());
     }
  }
%>

</form>
</body>
</html>

Products.java

public class Products {
    private String productname;
    public String getProductname() {
        return productname;
    }
    public void setProductname(String productname) {
        this.productname=productname ;
    }
}

ProductList.java(servlet-code)

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;


public class ProductList extends HttpServlet {

    static final String dbURL = "jdbc:mysql://localhost:3306/pdt";
    static final String dbUser = "root";
    static final String dbPass = "root";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter();
        ResultSet rs = null;
        Connection connection = null;   
        List<Products> pdt = new ArrayList<Products>();

        try{
            String company =request.getParameter("company");
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection (dbURL,dbUser,dbPass);
            String sql="select product_pck from comp_pdt_list where company_name='"+company+"'";
            PreparedStatement prep = connection.prepareStatement(sql); 
            rs=prep.executeQuery();

            while(rs.next()) { 
                Products prod=new Products();
                prod.setProductname(rs.getString("product_pck"));
                pdt.add(prod);
                request.setAttribute("list",pdt);
                RequestDispatcher rd=request.getRequestDispatcher("Productlist.jsp");    
                rd.forward(request,response); 
                return;
            }

            prep.close();
        } catch(Exception E) {
            //Any Exceptions will be caught here
            System.out.println("The error is"+E.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception ex) {
                System.out.println("The error is" + ex.getMessage());
            }
        }
    }
}

You set the attribute to request in your while loop. So the "list" contains only one product. (method returns on first iteration)

Add products to the list in while loop and set your list (request attribute) only after while loop.

Following should fix it:

while(rs.next()){ 
        Products prod=new Products();
          prod.setProductname(rs.getString("product_pck"));
          pdt.add(prod);           
}
request.setAttribute("list",pdt);

RequestDispatcher rd=request.getRequestDispatcher("Productlist.jsp");    
rd.forward(request,response); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM