sorting by clicking a link

Posted on 2006-04-25
Medium Priority
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

fargo earned 600 total points
ID: 16534524
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/yourpage.do?sorting=country&orderby=desc

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

WelkinMaze earned 600 total points
ID: 16534575
You can use history.go(0) to reload the page.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
With more and more companies allowing their employees to work remotely, it begs the question: What are some of the security risks involved with remote employees and what actions should we take to secure them?
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 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