Hi Jayant,

Actually, the Pager Tag Library is compatible with each design you
mentioned. I'll explain how you might implement for each.

On Tue, Sep 26, 2000 at 01:53:46PM -0700, Shekhar, Jayant wrote:
> Thanks for your reply Christopher.
>
> Here are my thoughts:
>
> * Execute a new query each time. Have a sort clause in the query, and also
> have a greater than clause on some field. Each time extract the first 100
> results. ( sort by id and id > x. the current value of x gets embedded in
> the hyperlink "Next")
>
> Advantages are that it is clean, and doesn't require much memory on the
> server side.
> Disadvantages are that it needs to reexecute the query each time. Also
> sorting on a text column would slow down the system.

<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>

<pg:pager maxItems="1000" maxPageItems="100" isOffset="true">
<pg:param name="keywords"/>
<%
  // PostgreSQL example:
  //
  //   select * from mytable
  //   where keywords = '<%= request.getParameter("keywords") %>'
  //   order by id
  //   limit <%= pagerMaxItems %>, <%= pagerOffset %>
  //
%>
  <% while (resultSet.next()) { %>
    <pg:item>
      <% // output record html ... %>
    </pg:item>
  <% } %>

  <pg:index>
    <pg:prev>
      <a href="<%= pageUrl %>">[ Previous ]</a>
    </pg:prev>
    <pg:pages>
       <a href="<%= pageUrl %>"><%= pageNumber %></a>
    </pg:pages>
    <pg:next>
      <a href="<%= pageUrl %>">[ Next ]</a>
    </pg:next>
  </pg:index>
</pg:pager>


> * Save the result set each time, along with the session id. With each page
> scroll, get the result set and start using it.
>
> Advantages are that it would be fast, and also would not require too much of
> extra memory.
> Disadvantages are that the result set would keep a connection to the
> database. Now, questions arise on as to when do you release this connection,
> and many hits can make the servlet run out of connections.


First, you could only have a next button unless your JDBC driver supports
scrollable ResultSets. If you were only going to have a next button the
advantages of the Pager Tag Library are lost.

If you do have a scrollable ResultSet, then here is how it could work.

<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>

<%
  ResultSet resultSet = session.getValue("resultSet");

  if (resultSet != null) {
    resultSet.first();
%>
<pg:pager maxPageItems="100">

  <% while (resultSet.next()) { %>
    <pg:item>
      <% // output record html ... %>
    </pg:item>
  <% } %>

  <pg:index>
    <pg:prev>
      <a href="<%= pageUrl %>">[ Previous ]</a>
    </pg:prev>
    <pg:pages>
       <a href="<%= pageUrl %>"><%= pageNumber %></a>
    </pg:pages>
    <pg:next>
      <a href="<%= pageUrl %>">[ Next ]</a>
    </pg:next>
  </pg:index>
</pg:pager>
<% } %>


> * Extract the whole result and save it in memory along with the session id.
>
> Advantages are that it would easily release the database connection.
> Disadvantages are that it would require a whole lot more memory, and also
> when do you dump the memory thus grabbed. Also, the first page would be slow
> because it would get all the matching results out of the database.

In this example the code expects the previously saved data in a CachedRowSet
stored in the session.

<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>

<%
  CachedRowSet cachedRowSet = session.getValue("cachedRowSet");

  if (cachedRowSet != null) {
    cachedRowSet.first();
%>
<pg:pager maxPageItems="100">

  <% while (cachedRowSet.next()) { %>
    <pg:item>
      <% // output record html ... %>
    </pg:item>
  <% } %>

  <pg:index>
    <pg:prev>
      <a href="<%= pageUrl %>">[ Previous ]</a>
    </pg:prev>
    <pg:pages>
       <a href="<%= pageUrl %>"><%= pageNumber %></a>
    </pg:pages>
    <pg:next>
      <a href="<%= pageUrl %>">[ Next ]</a>
    </pg:next>
  </pg:index>
</pg:pager>
<% } %>


> James, I also think that using "The Pager Tag Library" would also have this
> disadvantage of having to get all the matching results out of the database,
> so I'm not sure if its the right solution.

Since the Pager Tag Library is flexible and leaves the data source decision
up to you, it does not itself have any inherent disadvantages.

-James

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to