2

I am tring to display Arraylist of bean objects into an HTML table (JSP Page). Please help me where I am going wrong. This is the code which is creating a bean object which have the data from database. Finally all bean objects are added to a arraylist. using RequestDispatcher I am sending the arraylist to ViewData.jsp Now Please tell how to intepret the arraylist data into table.

package com.dsr.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import com.dsr.beans.EmpBean;
import com.dsr.db.DB;
import javax.servlet.RequestDispatcher;
import java.sql.*;

/**
 * Servlet implementation class for Servlet: CheckConn
 *
 */
 public class CheckConn extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public CheckConn() {
        super();
    }       

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }   

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        ArrayList<EmpBean> ebList= new ArrayList<EmpBean>();
        System.out.println(request.getParameter("date"));
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;



        try 
        {    
            DB dao=new DB();
            conn=dao.getConnection();  
            stmt=conn.createStatement();    

            rs = stmt.executeQuery("exec select_details");
            while(rs.next()){
                EmpBean eb = new EmpBean();
                eb.setEmp_id(rs.getString("emp_id"));
                eb.setFirst_name(rs.getString("first_name"));
                eb.setLast_name(rs.getString("last_name"));
                ebList.add(eb);
            }
            System.out.println(ebList.size());
            for (int i=0; i<ebList.size(); i++){
                EmpBean eb1= ebList.get(i);
                System.out.println(eb1.getEmp_id());
                System.out.println(eb1.getFirst_name());
                System.out.println(eb1.getLast_name());
            }
//          stmt.executeQuery("commit");

        }
        catch(Exception e)
        {        
            e.printStackTrace();    
        } 
        finally
        {
            try {
                conn.close();
                stmt.close();
                } catch (SQLException e) {

                e.printStackTrace();
            }


        }
        request.setAttribute("mybean",ebList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("viewData.jsp");
        if (dispatcher != null){
        dispatcher.forward(request, response);
        }

    }               
}

Now tell me how to inteprete the arraylist data into html table.

1 Answer 1

3

Use JSTL in JSP page:

For e.g.:

<table .... >
    <c:forEach var="empBean" items="${requestScope['mybean']}">
        <tr>
            <td>
                <c:out value="${empBean.first_name}"/>
            </td>
        </tr>
    </c:forEach>
</table>

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.