【问题标题】:To add the result set values from the servlet to the check box in jsp将 servlet 中的结果集值添加到 jsp 中的复选框
【发布时间】:2016-09-21 20:30:23
【问题描述】:

在不使用 JSTL 实现的情况下,我需要帮助将 servlet 中的结果集值添加到 jsp 中的复选框

工作流程:

1.用户在文本框中输入一个值并点击搜索按钮

2.单击搜索时调用 servlet。 servlet 专注于数据库实现并将结果集值转发到请求来自的同一 jsp 页面。

3.结果集值在jsp中应以复选框的形式显示

问题:

结果集值正在我的 jsp 页面中打印。我需要在我的 jsp 页面中将结果集值显示为复选框值,而不是打印所有值。我的结果集大小是 3。

这是我的代码:

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>

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();
if(rs!=null)
 {
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());
            }
                }

}

}

【问题讨论】:

    标签: jsp servlets resultset


    【解决方案1】:

    试试这个:

    for(Products prod: pdts){
           out.println("<input type=\"checkbox\" name=\"prod\" value=\"" + prod.getProductname() + "\">" + prod.getProductname() + "<br>");
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 2017-01-05
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多