Display ArrayList elements using Iterator in JSP

By | June 25, 2018

In this example, a user requests to display employee’s data belonging to an organization on a UI (JSP).  This example uses the MVC architecture. Following steps summarizes this example. 

  1. The JSP contains a link to request for the employee data (View)
  2. The request is sent to a Java servlet (Controller)
  3. The servlet calls a DAO class to execute the business logic. In our case, it is to fetch employee data (Model)
  4. The request again sent to the JSP to display the employee data. The data is held in the ArrayList here.
  5. Next, display ArrayList elements using Iterator in JSP.

The following image explains the files/classes used in this example. Do note that, you would need servlet-api.jar to add dependencies for the Java servlet. We are using the Eclipse Integrated development environment(IDE) for this example. You can get one for free from here. To begin with, go to File > New > Dynamic Web Project

To display ArrayList elements using Iterator in JSP

Directory Structure



 

The following class EmployeeDetails.java pushes the employee data into the arraylist object. Please note that you can also use a database connection to read employee data and assign it to the arrayList object. You can also make use of JavaBeans to play with the employee attributes. You can go through the following links to understand how you can connect to a database and use JavaBeans. Login application in Java and Registration application in Java.

The web.xml contains the servlet information. This file gets loaded in the beginning when you run your project. This is also known as deployment descriptor. It can also contain additional tags to define the session interval, welcome file lists and many more.

The following image explains how the Employee data looks like on the UI dashboard (JSP).

Iterator Example implementation

JSP showing the end result

Suggested articles for you:

Did it help? Comment here..

This site uses Akismet to reduce spam. Learn how your comment data is processed.