user9539699
user9539699

Reputation: 1

Displaying data table from database on jsp in the form of table using servlet in java mvc?

I am developing website using servlet and jsp.I want to retrieve data from a table and display it on a table in the jsp, but jsp should not contain any processing code. All processing code should be in servlet. Can anyone please help me out.

Upvotes: 0

Views: 1215

Answers (2)

Rishal
Rishal

Reputation: 1538

Do the database query in some servlets and obtained results you should set in the request using below statements,

ArrayList<Table_8> data = (ArrayList) dao.select();
request.setAttribute("databaseResults", databaseResults);
request.getRequestDispatcher("xyzJsp.jsp").forward(request, response);

Now in your JSP, use something like below,

<c:forEach items="${requestScope.databaseResults}" var="element" varStatus="loop">
  <tr>
    <td>${loop.index+1}</td>
    <td>${element.title}</td>
    <td>${element.platform}</td>
    <td>${element.score}</td>
    <td>${element.genre}</td>
    <td>${element.editorsChoice}</td>
  </tr>
</c:forEach>

where element is a type of valueobject/POJO class, which consists of all those attributes.

For more information please check Web application using Servlets,JSP,Bootstrap and database

Upvotes: 0

cisk
cisk

Reputation: 618

You need to perform queries in your servlet and put your result in a place accessible also to the jsp (like session or request attributes). Then in your jsp you can loop the result (try searching on google how to use jstl taglib) and print them as you wish. Hope this helps.

Found this on github that may help you: https://github.com/danielniko/SimpleJspServletDB

Upvotes: 1

Related Questions