1

I don't understand how to pass and display the values to a jsp obtained in the servlet using ResultSet. I saw many examples. I don't understand the foreach loop. Why are there two variables? Does no one use the jsp:useBean anymore?

How do I pass and display the data. I tried using the bean class but the data in ResultSet is a table and I don't know how to set and get the values of all rows. Please help.

This is the servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    Connection connection = null;
    Statement stmt = null;



    try {
        StringBuffer data = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DesktopScreen","sa","sa123");
        stmt = connection.createStatement();

        String query = "select * from ClientLogin";
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String UniqueId,ClientId,RequestedDateTime,ConnectionStatus;
            UniqueId = rs.getString("UniqueID");
            ClientId = rs.getString("ClientId");
            RequestedDateTime = rs.getString("RequestDateTime");
            ConnectionStatus = rs.getString("ConnectionStatus");
            BeanClass beanClass = new BeanClass();
            beanClass.setUniqueId(UniqueId);
            beanClass.setClientId(ClientId);
            beanClass.setConnectionStatus(ConnectionStatus);
            beanClass.setRequestDateTime(RequestedDateTime);

            data = new StringBuffer();
            data.append(UniqueId);
            data.append(ClientId);
            data.append(RequestedDateTime);
            data.append(ConnectionStatus);


        }
        request.setAttribute("data", data);
        request.getRequestDispatcher("Display.jsp").forward(request, response);
    }
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {

        out.println("</center>");
        out.print("</table>");
        out.println("</body>");
        out.println("</html>");
        out.close();
        try {
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }



}

}

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • Food for read: http://stackoverflow.com/tags/servlets/info and http://stackoverflow.com/q/3177733 – BalusC Mar 01 '16 at 13:42

1 Answers1

1

You could put the resultSet in a List and pass it to your page and in JSP extract this list

Servlet :-

List <BeanClass> myList = new ArrayList<>();
while (rs.next()) {
            String UniqueId,ClientId,RequestedDateTime,ConnectionStatus;
            UniqueId = rs.getString("UniqueID");
            ClientId = rs.getString("ClientId");
            RequestedDateTime = rs.getString("RequestDateTime");
            ConnectionStatus = rs.getString("ConnectionStatus");
            BeanClass beanVar= new BeanClass();
            beanVar.setUniqueId(UniqueId);
            beanVar.setClientId(ClientId);
            beanVar.setConnectionStatus(ConnectionStatus);
            beanVar.setRequestDateTime(RequestedDateTime);
            myList.add(beanVar);
}
request.setAttribute("list", list);
request.getRequestDispatcher("Display.jsp").forward(request, response);

In jsp :-

use this tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Access the list using forEach

<table>
    <c:forEach items="${list}" var="item">
        <tr>
            <td>${item.getUniqueId()}</td>
            <td>${item.getClientId()}</td>
            <td>${item.getConnectionStatus()}</td>
            <td>${item.getRequestDateTime()}</td>
        </tr>
    </c:forEach>
</table>
karim mohsen
  • 2,164
  • 1
  • 15
  • 19