Question

i am fetching the data from database by servlet and then setting it to resultset. Now i want to dislay data on the jsp page. for that i used list to set all data and with jstl i displayed at jsp page. but i am getting two column with same data. Here is the code..

SERVLET CODE:

ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {

                wb.setDeviceAccount(rs.getString("accountID"));
                wb.setVehicleId(rs.getString("deviceID"));
                wb.setSimNumber(rs.getString("simID"));
                wb.setImeiNumber(rs.getString("imeiNumber"));
                wb.setLastTimestamp(rs.getString("lastGPSTimestamp"));
                wb.setLastLoginTime(rs.getString("lastUpdateTime"));
                wb.setExpirationTime(rs.getString("expirationTime"));
                list.add(wb);

            }
            rs.close();
            stmt.close();
            con.close();
            request.setAttribute("deviceList", list);

            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/installedDeviceList.jsp");
            requestDispatcher.forward(request, response);

JSP PAGE:

<c:forEach items="${deviceList}" var="dList">
                <tr>
                    <td></td>
                    <td>${dList.deviceAccount}</td>
                    <td>${dList.vehicleId}</td>
                    <td><a href="#" onclick="toggle();">${dList.simNumber}</a></td>
                    <td>${dList.imeiNumber}</td>
                    <td>${dList.lastTimestamp}</td>
                    <td>${dList.lastLoginTime}</td>
                </tr>
            </c:forEach>
Was it helpful?

Solution

IN your loop always create new reference of Wb. here i am assuming Wb is your class name.you can change code based on your class name.

Wb wb=null;


ResultSet rs = stmt.executeQuery(query);    
            while (rs.next()) {   

                wb =new Wb();  

                wb.setDeviceAccount(rs.getString("accountID"));
                wb.setVehicleId(rs.getString("deviceID"));
                wb.setSimNumber(rs.getString("simID"));
                wb.setImeiNumber(rs.getString("imeiNumber"));
                wb.setLastTimestamp(rs.getString("lastGPSTimestamp"));
                wb.setLastLoginTime(rs.getString("lastUpdateTime"));
                wb.setExpirationTime(rs.getString("expirationTime"));
                list.add(wb);

            }

OTHER TIPS

You are creating only one object apparently while you should be creating a unique 'wb' instance for each row. You don't have two duplicate rows, you simply add the exact same object multiple times to the list.

For each of the row in result set, create a new wb object to store data from resultset and add the same to list.

Example:

while ( rs.next() ) {
    // set this to equivalent class on your wb variable.
    // wb = new wb();

    wb.setDeviceAccount( rs.getString( "accountID" ) );
    // read other column values here and set in wb
    // ...

    // lastly
    list.add( wb );
} // while rs

Then on your web page, you will get each record read correctly from the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top