0

Hi every one can any help me ...

i want to list all the data from data base and display to jsp table

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub


    HttpSession session = request.getSession(true);

    try {
        Connection con=DataConnection.getconnection();
        PreparedStatement pst;
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        pst = con.prepareStatement("select * from [user]");
        ResultSet rs = pst.executeQuery();
        List<User> ee = new ArrayList<User>();

        while (rs.next()) {
            User e = new User();
            e.setName(rs.getString(1));
            e.setUserName(rs.getString(2));
            e.setPass(rs.getString(3));

            ee.add(e);
        }

        request.getSession().setAttribute("ee", ee);
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/user.jsp");
        rd.forward(request, response);

    } catch (Throwable theException) {
        System.out.println(theException);
    }
}

this my code it contain the array list and passing it trought request.getSession().setAttribute("ee", ee);

now how to acess this value in jsp and it should display in the from of table can any give me the sample code for this

seenukarthi
  • 8,241
  • 10
  • 47
  • 68

2 Answers2

0

Use JSTL

I would suggest to set data in request instead of session.

replace line

 request.getSession().setAttribute("ee", ee);

with

 request.setAttribute("ee", ee);

than use code below in your JSP

<c:forEach items="${ee}" var="element"> 
  <tr>
    <td>${element.col1}</td>
    <td>${element.col2}</td>
  </tr>
</c:forEach>
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
0

You can access the array using ${ee}.

There are serveral ways to iterate using JSTL.

Assuming your User.java contains for example two atributes: name and surname you can access to this information in your JSP with the following code:

    <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

    <c:forEach items="${sessionScope.ee}" var="item">
        ${item.name} - ${item.surname} 
    </c:forEach>

You should take into account that you are putting your attribute in session. (that's why you have to use ${sessionScope}) Make sure if you want to use session or not.

Oscar Ferrer
  • 126
  • 1
  • 4