将结果集值从servlet显示到jsp

时间:2016-05-23 10:08:44

标签: java jsp servlets resultset

我需要帮助将结果集值从servlet转发到jsp而不使用JSTL实现

工作流程

  1. 用户在文本框中输入值并单击搜索按钮
  2. 在单击搜索时,将调用servlet。 servlet专注于数据库实现,并将结果集值转发到请求所在的同一jsp页面。
  3. 问题: 我的结果集大小为3,但仅在我的表顶部的值将打印在我的jsp页面中。剩下的2个值都缺失了。我想在jsp页面中打印所有值。

    这是我的代码:

    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的代码)

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

1 个答案:

答案 0 :(得分:1)

在while循环中将属性设置为request。所以&#34;列表&#34;仅包含一种产品。 (方法在第一次迭代时返回)

在while循环中将产品添加到列表中,并仅在while循环后设置列表(请求属性)。

以下应该修复它:

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