Code Monkey home page Code Monkey logo

jsp-serlet-pagination's Introduction

Pagination using JSP and Servlets

This is a little explanation of how to build application with pagination and data sorting using only JSP and Servlets. I will describe details about pagination engine, the best practice for database connection, what is Data Transfer Object (DAO) and Transfer Object, how to write servlet and bind it with JSP. Only one jstl.jar you need for project and it should be placed on the WEB-INF/lib directory.

In this example we will display a list of product’s details like name and price that is bind with appropriate group. Left panel contains a list of group, right panel contains a list of product of the selected group. Near the name of each group display a products count. Some of group can be empty, other contains a thousand of products. Thats why we should display not more that 10 products on the page.

It’s necessary to show the current page and navigation elements for moving to the next and previous page. Also we will add sorting for products column in ascending and descending order. If the user clicks on the header column(product name or price) the first time that column should be sorted in ascending order and if the action is repeated on the same header column then the column should be sorted in descending order.

A little bit about pagination and what method we will choose? In our case pagination means that large document will be breaking into separate pages for viewing. Basically it’s a way of making a website with a lot of data more user-friendly and manageable.

There are different ways of pagination. First way, not very good as for me based on getting all records at once. Later when user needs result, some part of data can be retrieved from the cache instead of querying the database to fetch the next set of results. The negative side of this approach is that the data becomes stale. And also fetching a lot of data from database requires many resources(CPU and memory). Nobody likes a slow website, faster is always better. And the second way more interesting approach. We get a range of records every time we need and display it to the user, just limit the result of data that we need to display. This approaches solves a lot of problems. And we will build application that way.

  • This example uses two tables Group and Product. Here is the link to the mysql database dump file.

  • Transfer object classes that will encapsulates our business data.

  • Connection factory help us to make connection to the database. And of course it’s a singleton.

  • Data Access Object or DAO encapsulates database operations.

  • Servlet processes different request parameters like group of products, current page, desc/asc sorting. Also we are storing a lot of attributes in the request scope and then forwarding request to a JSP. Here is the code of Servlet.

  • Only one JSP page that retrieves the attributes from request and display the result. Easier to nowhere! :)

In summary, here is the project structure and other screenshots.

jsp-serlet-pagination's People

Contributors

dmitrynikol avatar

Watchers

YS avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.