0

I'm trying to gain some performance on my application that search members in groups in ldap. My page shows 5 drop down lists which countains groups from different domains (around 150+ in each). I noticed that my application spend most of the time loading those drop down lists and little time doing the actual search. I was wondering if there is a way to load them once and then use them until the session is closed. Sample of code from my servlet :

public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

...

 // Get the groups for the 5 drop down lists
    dropdownlist1 = gvf.getGroupes1();
    dropdownlist2 = gvf.getGroupes2();
    dropdownlist3 = gvf.getGroupes3();
    dropdownlist4 = gvf.getGroupes4();
    dropdownlist5 = gvf.getGroupes5();

// Set the attributes to return to the vue
    request.setAttribute( ATT_FORM, gvf );

// Session scope attributes ???    
    HttpSession session = request.getSession();
    session.setAttribute( ATT_GROUP1, dropdownlist1 );
    session.setAttribute( ATT_GROUP2, dropdownlist2 );
    session.setAttribute( ATT_GROUP3, dropdownlist3 );
    session.setAttribute( ATT_GROUP4, dropdownlist4 );
    session.setAttribute( ATT_GROUP5, dropdownlist5 );

}   

So I'm not sure if I need to add some code to my jsp or how to test is this is working. I'm using eclipse and with the debugger I see that the 5 getGroupes functions are called everytime I load the page instead of once. Am I missing something ?

edit: I'm pretty sure I'm missing some kind of "if drop down list not loaded --> load drop down list, else continue"

trixrabbit
  • 259
  • 7
  • 22
  • I forgot to mention how my application works : you select a group from the drop down lists and click a button (search) and the page reloads with the members of the group appearing under the 5 drop down lists. In my case the lists are repopulated everytime. Also another page with groups in href refers to my group search page. Whenever I click on a link from that page, it sends me on the group search page as if I selected the group from a drop down list. This still reloads the lists. I wish it was simple to load the lists once and as long as the user doesn't close the page, they stay loaded – trixrabbit Jun 03 '15 at 18:10
  • Is this acceptable as dupe? http://stackoverflow.com/questions/2263996/populating-child-dropdownlists-in-jsp-servlet Go for the ajax way. – BalusC Jun 03 '15 at 20:24
  • I read that yesterday but couldn't understand everything. I never used javascript so I'm learning that right now...I already use jstl to fill the lists in my jsp. Is the person in the thread you linked using a separate servlet/form just to fill the drop down lists? – trixrabbit Jun 04 '15 at 12:40

2 Answers2

0

You have to be careful about what you store in the session because it does not scale well. Your doing a look up and creating a copy for every user on the first visit.

A better alternative would be to cache your results with something like Memcached: http://memcached.org/

Very easy to implement. Quick example right on the home page.

Rob Kraft
  • 216
  • 3
  • 6
  • I'm currently testing on localhost, I noticed weird behavior in my console with the session scope so I changed it back to request for now. I'll look into memcached but I'd prefer to avoid using many different api. I thought that java ee had a way of doing this – trixrabbit Jun 03 '15 at 16:42
  • From what I read about memcached it seems a bit overkill of a setup for 5 drop down lists...Would there be a solution with javascript? – trixrabbit Jun 03 '15 at 16:49
  • Not sure about that. Your code is executing server side before the browser loads the page. – Rob Kraft Jun 03 '15 at 17:43
0

I actually tried something which seems to be working, I'm not sure if it is good practice but that's all I understand for now. The code below is only for one drop down list to show the principle :

HttpSession session = request.getSession(true); // get the current session or create a new one is no session found
dropdownlist1 = (List<Group>) session.getAttribute(ATT_GROUP1); //get the List from session scope

if (dropdownlist1 != null) {

    request.setAttribute(ATT_BUR_LIST, bur_list); // set the attribute for the vue

} else {

    dropdownlist1 = gvf.getGroups1(); // get the List of groups
    session.setAttribute(ATT_GROUP1, dropdownlist1 ); // set the session attribute so next time I don't have to load the List again

}

Note that I'm using jstl to create the drop down list in my jsp using

This seems fine from the debugger, it also looks faster in my browser. I welcome any kind of critic/improvement on this method.

trixrabbit
  • 259
  • 7
  • 22