sorting by clicking a link

Hi Experts,
I have a jsp page(the view) as shown below. It displays beans that are coming from a servlet.
I would like have the header of my table used to display the bean as link so that when clicked, it sorts the beans by that field. I know how to implement the method that sorts a list of bean(as shown below ).

My question: How can i setup that link so that when clicked the jsp view is reloaded and the tabel is sorted by the field clicked?
==================== Sorter implementation =============
class CountryComparator implements Comparator { public int compare(Object
        obj1, Object obj2) { IpInfoBean bean1 = (IpInfoBean) obj1; IpInfoBean
        bean2 = (IpInfoBean) obj2; return ( bean1.getCountry().trim()
        ).compareToIgnoreCase( bean2.getCountry().trim() ); }

and called as: Collections.sort( listOfBean, new CountryComparator() );
================= the jsp view ===================
<jsp:useBean id="listOfipInfo" type="java.util.ArrayList<IpInfoBean>" scope="request"/>
<b>Total # of IP: <%= request.getAttribute("numOfCountry")%></b><br>
<table border="1">
      <th># of Country/IP</th>
<% for(IpInfoBean b : listOfipInfo ) { %>
      <td><%= b.getIps() %></td>
      <td><%= b.getCountry() %></td>
      <td><%= b.getCountryCount() %></td>
Who is Participating?
for the header column u wish to have the sorting on the server side, have a link on each header column which points to the loading of the page (the way u come to this page initially) and add a parameter like http://localhost:8080/myapp/

u need to check on the action class or servlet for the sorting parameter and do the server side handling of comparater with the list. You may add the orderby as per your needs(sure u have to handle it !)

You can use history.go(0) to reload the page.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.