The servlet gets the data from the db for the correct page (using suitable SQL syntax for the query), and then turns the result set into a List of objects of a class representing that data. It is this List that the JSP will loop over and display.

The "next" and "previous" page buttons will simply fire a call to the servlet with the correct page number (currentPage + 1 for next, currentPage - 1 for previous).

Java mixed into JSPs like that is very difficult to read, and difficult to debug.

For starters, right at the bottom of the code you post you have the connection and resultset close() calls back to front...and appear to have completely missed the close() for the statement. And they're not in a finally block.

This is something that's a lot harder to miss in a properly structured app (though people still succeed in doing that).

Whether this is a portal or not is no excuse for abusing a JSP. Is this some sort of project or company policy to write code like this then?