No Widget Added

Please add some widget in Offcanvs Sidebar

Shopping cart

shape
shape

Servlet Pagination

Servlet Pagination What is Servlet Pagination? Java Servlet Pagination Example of Servlet Pagination

 Servlet pagination is a technique used to split a large list of records into smaller subsets, allowing users to view only a portion of the data at a time. Let’s walk through an example of implementing pagination in a Java servlet:

Create a MySQL Table: First, create a table in your MySQL database to store employee records. For instance:

CREATE TABLE employee (
    empid INT(11),
    empname VARCHAR(20),
    empsalary INT(11),
    empdept VARCHAR(20)
);

JavaBean Class: Next, create a JavaBean class (e.g., Employee) to set and retrieve values from the database. Here’s a simplified version:

public class Employee {
    private int employeeId;
    private String employeeName;
    private int salary;
    private String deptName;

    // Getters and setters
    // ...
}

Connection Factory: Implement a factory class (Connection Factory) to manage database connections. This class will handle connecting to the MySQL database:

public class ConnectionFactory {
    // ...
    public Connection getConnection() throws SQLException, ClassNotFoundException {
        // Establish a database connection
        // ...
    }
}

EmployeeDAO: Create a DAO (Data Access Object) class (EmployeeDAO) responsible for fetching employee records. It will use the ConnectionFactory to connect to the database and retrieve data:

Java

public class EmployeeDAO {
    // ...
    public List<Employee> getEmployees(int offset, int limit) {
        // Execute a query to fetch employees with pagination
        // ...
        // Add retrieved records to an ArrayList<Employee>
        // ...
        return employeeList;
    }
}

Servlet Implementation: In your servlet, handle pagination by specifying the offset (starting index) and limit (number of records per page). For example:

Java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    int currentPage = Integer.parseInt(request.getParameter("page")); // Current page number
    int recordsPerPage = 10; // Number of records per page

    int offset = (currentPage - 1) * recordsPerPage;
    List<Employee> employees = employeeDAO.getEmployees(offset, recordsPerPage);

    // Set the employee list as an attribute in the request
    request.setAttribute("employees", employees);

    // Forward to the JSP page for rendering
    request.getRequestDispatcher("employee-list.jsp").forward(request, response);
}

JSP Page (employee-list.jsp): Finally, create a JSP page to display the paginated employee records. Iterate through the list and render the data accordingly:

<table>
    <thead>
        <tr>
            <th>Employee ID</th>
            <th>Name</th>
            <th>Salary</th>
            <th>Department</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach items="${employees}" var="employee">
            <tr>
                <td>${employee.employeeId}</td>
                <td>${employee.employeeName}</td>
                <td>${employee.salary}</td>
                <td>${employee.deptName}</td>
            </tr>
        </c:forEach>
    </tbody>
</table>

Remember to adjust the code according to your specific requirements and database configuration. Pagination ensures that large datasets are manageable and improve user experience by displaying relevant data in smaller chunks.

Leave A Comment

Your email address will not be published. Required fields are marked *