sorting by clicking a link

Posted on 2006-04-25
Last Modified: 2012-05-05
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>
Question by:komlaaa
    LVL 11

    Accepted Solution

    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 !)

    LVL 11

    Assisted Solution

    You can use history.go(0) to reload the page.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Outlook Free & Paid Tools
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now