将arraylist中的元素显示为servlet

时间:2016-10-15 18:54:36

标签: java jsp servlets arraylist

当我尝试将Arraylist中的元素显示到我的servlet中时,我遇到了问题。当我尝试打印列表的大小时,它可以工作,但是我无法从控制台中获得该列表中的元素。

MyServlet

public BoardServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @param currentCategoryID 
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    int currentCategoryID =  Integer.parseInt(request.getParameter("currentCategoryID").toString());
    List listBoards = DBoard.getBoardByCategory(currentCategoryID);

    request.setAttribute("boards",listBoards);


    ArrayList<Board> list =new ArrayList<Board>();
    //storing passed value from jsp
    list = (ArrayList<Board>)request.getAttribute("boards");
    System.out.println(list.size());

    for(Board board : list) {

        System.out.println(board.getNameBoard());
    }

    getServletContext().getRequestDispatcher("/Board.jsp").forward(
            request, response);             

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



}

MyDaoClass

public static  List<Board> getBoardByCategory(int idCategory) {
    Connection con = null;
    int count=0;
    List<Board> boardList = new ArrayList<Board>();
    try {
        con = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Cristian\\workspace\\Forum Application1\\database.db");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        String sql = "SELECT * FROM Board WHERE idCategory=?";
        PreparedStatement statement = con.prepareStatement(sql);
        statement.setInt(1, idCategory);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            count++;
            int id_Board = rs.getInt("idBoard");
            int id_Category = rs.getInt("idCategory");
            String board_name = rs.getString("nameBoard");
            Board obj = new Board(id_Board,id_Category,board_name);
            boardList.add(obj);         

        }
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if(count==0) {boardList=null;}
    return boardList;
}
 private static Connection connect() {
        Connection con = null;
        try {
            Class.forName("org.sqlite.JDBC");
            try {
                con = DriverManager.getConnection("jdbc:sqlite:C:\\Projects\\RSA80Workspace\\Forum Application\\database.db");
                System.out.println("connected!");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }

0 个答案:

没有答案