wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to allow sorting of table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to allow sorting of table?" Watch "How to allow sorting of table?" New topic
Author

How to allow sorting of table?

tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Hi,

I managed to create a servlet which allow user to view the table as per my Msaccess table.


The data from the Ms acces table is entered using a html form.

However, I am stuck on how to allow sorting of table using the same page that show the viewing of table.

I would appreciate it if someone can advise me how to go about doing this. Any example codes would help me because I am totally lost.

Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Do you want to sort based upon clicking a table column header (the usual means)?

If so:
  • Establish a click handler on the headers.
  • In the handler, add the name of the column to be sorted to a hidden input and submit the form.
  • On the server, use the DB to sort on the specified column.
  • Display the newly sorted list.


  • Needless to say, all the client-side interaction requires JavaScript.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    Hi,

    May I know how to :

    1.Establish a click handler on the headers.

    - Do I add something in my html form page a javascript function to do that? I just need to sort one column will do like the usertype because I have 2 types inside.

    2.In the handler, add the name of the column to be sorted to a hidden input and submit the form.

    3.On the server, use the DB to sort on the specified column.

    - Does that mean I still have to add a sql statement to do the sorting of that specific column ? What I want is that when the table is being displayed, the user can sort by clicking on the column header.

    4. Display the newly sorted list.

    Million thanks.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39547
        
      27
    This sounds like a good use case for the DisplayTag library: http://displaytag.sourceforge.net/1.2/ (assuming that you're using JSP for the view).


    Ping & DNS - updated with new look and Ping home screen widget
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    I am using java servlet to get the data from the ms access table and display it. the html codes are embeded inside the servlet. Using sql I just do a search * and get all to display.

    I went to that page that you show me but I do not know how to use that because I have not learnt how to use MVC model yet.

    Can you tell me what are the steps to do the sorting table using the simplest method. tks.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39547
        
      27
    tangara goh wrote:the html codes are embeded inside the servlet.

    That's the first thing you may want to change - move the HTML into JSP pages, and keep only the actual logic in the servlet.

    I went to that page that you show me but I do not know how to use that because I have not learnt how to use MVC model yet.

    MVC is how web apps (and other apps) are built these days. The sooner you start learning about it, the better. Here's an article that may help you get going: http://www.javaranch.com/journal/200603/frontman.html
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    so you are saying that it's better to use response.redirect inside my servlet to a jsp page that display my table?

    I do not know if I have the time to learn mvc due to time constraint. I need a quick solution for now.

    However, I would like to pick up the mvc when my project is ok.

    Can you help me with the sorting part? Tks.
    Vijitha Kumara
    Bartender

    Joined: Mar 24, 2008
    Posts: 3775

    tangara goh wrote:so you are saying that it's better to use response.redirect inside my servlet to a jsp page that display my table?

    Since you already have the servlet just move the HTML in to the JSP and forward the request [RequestDispatcher#forward(request,response)] to the JSP which will render the output. The main purpose is to avoid the HTML inside the servlet as Ulf pointed out.


    SCJP 5 | SCWCD 5
    [How to ask questions] [Twitter]
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    I am sorry but please tell me where exactly to insert the above forward request dispatch.

    Is it under } finally {
    pw.close();
    }
    }

    and before

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    // response.sendRedirect("displayData.jsp");
    }

    /**
    * Returns a short description of the servlet.
    * @return a String containing servlet description
    */
    @Override
    public String getServletInfo() {
    return "Short description";
    }// </editor-fold>

    Can you also advise me my code for the jsp page is it correct ?

    <body>
    <form action="searchResult2" method="get">
    <div id="header">Welcome to Care & Concern</div>
    <h4>View Member Record</h4>
    <div id="center" class="column">
    <table border="1" style="padding:3px;" align="center" >
    <tr>
    <td>Name value= "txtFullName" </td>
    <td>Email value="txtEmail"</td>
    <td>Address name="txtAddress"</td>
    <td>NRIC value="txtNRIC" </td>
    <td>Contact name="txtContact"</td>
    <td>Gender name="txtGender"</td>
    <td>Type name="txtType" </td>
    <td>Remarks "text" value="txtRemarks"</td></tr>
    </table>
    </form>

    I wonder how can the jsp page read the data forward from the servlet. Please advise. Many thanks.
    Praveen mourya Kumar
    Greenhorn

    Joined: Oct 14, 2008
    Posts: 16
    tangara goh wrote:so you are saying that it's better to use response.redirect inside my servlet to a jsp page that display my table?

    I do not know if I have the time to learn mvc due to time constraint. I need a quick solution for now.

    However, I would like to pick up the mvc when my project is ok.

    Can you help me with the sorting part? Tks.


    If i assume, you want to display the date from the MSAccess to client. And when the client click on the Column header(like Cust_ID, Name) for Ascending or descending order, then you want all the data should get sorted accordingly.
    Better you use, Javascript. Store all the table values in multi-dimmensional array. When the user click on the sort. then you have to change the view of your page through rearranging the arrays.It will be according to column header. In this way, you can avoid going into the MVC or other pattern.
    Moreover, sending request to another Servlets will again time consuming.


    SCJP 1.6..
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    Thanks but sorry ...One step at a time. My table in the html codes above correct? Can it read the data from the servlet?
    Vijitha Kumara
    Bartender

    Joined: Mar 24, 2008
    Posts: 3775

    tangara goh wrote:I am sorry but please tell me where exactly to insert the above forward request dispatch.

    After you set the data in appropriate scope(s) (read below) you can forward the request to the JSP. Check the RequestDispatcher API.

    I wonder how can the jsp page read the data forward from the servlet. Please advise. Many thanks.

    You should do the necessary logic inside the servlet (either doPost()/doGet()) and set the data needed by the JSP in an appropriate scope (that is on of ServletRequest, HttpSession, ServletContext - all these have methods for setting objects in those scopes like "setAttribute(String, Object)" check their APIs). Then it's just displaying data set by the servlet. You will need to know about EL, JSTL, Standard actions and Custom/Standard tags. But with EL, JSTL and standard actions only you get most of the basic things. And next time please use code tags when posting code here.
    Vijitha Kumara
    Bartender

    Joined: Mar 24, 2008
    Posts: 3775

    And check the library pointed out by Ulf above (which I have not used), It looks you can pass a list of objects and it'll handle most of the things.
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    Hi Vijitha,

    I have do the necessary do post do get http servlet request reponse steps.

    But, looking at the jsp or html that is going to display the table I find that there is something amiss. Can my jsp page really display the table?

    Can I leave out the api for the time being because now I just need to get the jsp page to disply the table?
    Vijitha Kumara
    Bartender

    Joined: Mar 24, 2008
    Posts: 3775

    tangara goh wrote:Can I leave out the api for the time being because now I just need to get the jsp page to disply the table?

    But I guess if you are going start all over with JSTL, EL etc.. (that is if you haven't use them before) it will take more time than using the library (of course I'm not sure how to use it). But the JspFaq contains good docs to get started if you can try.
    tangara goh
    Ranch Hand

    Joined: Dec 27, 2009
    Posts: 125
    Hi Vijitha, Could you advise if the request forward part is it correctly insert as per my below codes:


    I have to confess that I copied the code from somewhere else and I am not sure why I need to specify something behind the RequestDispatcher .

    Please advise.

    Many Thanks.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39547
        
      27
    Using a RequestDispatcher only makes sense if the output is generated by a JSP ... which it's not, since you're writing the HTML from within the servlet.

    Be advised that using a JSP (and/or JSTL and/or MVC and/or EL) does not fundamentally help with the problem of sorting. It merely ensures that there's a sound design in place (and, of course, once you're using JSP you could makes use of the DisplayTag library). You may want to work through a comprehensive servlet/JSP tutorial like http://pdf.coreservlets.com/ to pick up the pieces that you're still missing.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to allow sorting of table?
     
    Similar Threads
    Sorting in struts
    How to avoid server calls in Sorting
    Sorting table - Image issue
    How to fast sorting
    How to achive pagination +sorting table ?