Solved

jsp question

Posted on 2006-07-16
14
301 Views
Last Modified: 2013-11-23
Hi,
Im devlopping a website.

I have all the countries stored in my DB,
When a user wants to subscribe to the website, ha has to select his country.

I loaded the countries from my DB in memory in a "load-on-start" servlet.
I'm making a JSP file that gets the countries and display them in a combobox.

My question :

For every request,
the jsp file needs to get the countries and display them from a java class.
But I want the countries to be loaded in this combobox just once (not for every new request).

Thank you for any help.
0
Comment
Question by:matthew016
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17119377
not sure I follwo, every request is going to need to generate a response page so will need to generate the <select>.
Perhaps build the select in an include
0
 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 100 total points
ID: 17119688
Load all the country names for the first request and store it as a session attribute,

so for further requests check the session attribute, if its not null and has values use them to print in the combo box. you no need to query everytime.

like,
  Object names ;
if ( session.getAttribute ("CountryName") == null )
{

  names = getNames();
 session.setAttribute ( "CountryName", names ) ;
}

else
 names = session.getAttribute ( "CountryName' ) ;


displayNames () ;
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 100 total points
ID: 17120235
You can load your needed data once and cache it. You can cache your data in the session (per user) or in a static member of a class (per server).
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 17120416
   Hi!

Store the country-names in an EJB Session Bean and let the server handle the load and cache of the data.
In that way country-names are stored in memory at startup and can be accessed quickly.

Regards,
  Tomas Helgi
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17120428
I think an EJB Session Bean for this task is like using a rocket launcher to kill a fly.
0
 
LVL 24

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 50 total points
ID: 17120485
>I think an EJB Session Bean for this task is like using a rocket launcher to kill a fly.
Hmmm, yes you are right about that ;)

One other way to handle this is to create a HttpSessionListener and have it loaded in a static java-class/object as enachemc says.

Regards,
   Tomas Helgi  
0
 
LVL 9

Author Comment

by:matthew016
ID: 17120844
If I store them in a session, the countries need to be loaded for each user no?
I want to load them just for once.

So actually I could load them in a static class and then access this class with a loop in the select.
Well actually I already did that and I tought it was a problem for performance and that
I could find a better solution.

>Perhaps build the select in an include
why?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
ID: 17120886
> I want to load them just for once.

thought u said you'd already loaded them?

> So actually I could load them in a static class and then access this class with a loop in the select.

should load them into a static class.
you should load them once and store them in the application context

>>Perhaps build the select in an include
>why?

In case you want to add the select to >1 pages
0
 
LVL 9

Author Comment

by:matthew016
ID: 17120909
> I want to load them just for once.
>thought u said you'd already loaded them?

Actually I meant load them once from the application into my jsp page ...
I don't know if this affects performance because
at every request a big loop in the select is executed, and laod them from the class.
Then I have a loop to select language, and other loop.
U know what I mean ?

0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 17120916
As I said above, you can't avoid generating the select for each request, and it shouldn't create a performance concern to you anyway.
0
 
LVL 9

Author Comment

by:matthew016
ID: 17120928
Thank u very much
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 17120952
As I said before.
Create a HttpSessionListener class and put a static java-class/object in that class which then can be accessed from all JSP pages. Doing this you load the data ONCE when the web-app is first loaded and destroyed when no users are browsing the web by counting active users.
Do it something like this :

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionCounter implements HttpSessionListener {

      private static int activeSessions = 0;
                private static myCountry countries;  // this class holds the countries ;)
                private static boolean countries_loaded = false

      public void sessionCreated(HttpSessionEvent se) {
            activeSessions++;
                                if(activeSessions > 0 and !countries_loaded){
                                      countries_loaded = true;
                                      countries = new myCountry();
                                      ......
                                }
      }

      public void sessionDestroyed(HttpSessionEvent se) {
            if(activeSessions > 0)
                  activeSessions--;
      }

      public static int getActiveSessions() {
            return activeSessions;
      }
}

Regards,
    Tomas Helgi
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 17120983
Hmmm forgot one thing. ;)

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionCounter implements HttpSessionListener {

     private static int activeSessions = 0;
                private static myCountry countries;  // this class holds the countries ;)
                private static boolean countries_loaded = false

     public void sessionCreated(HttpSessionEvent se) {
          activeSessions++;
                                if(activeSessions > 0 and !countries_loaded){
                                      countries_loaded = true;
                                      countries = new myCountry();
                                      ......
                                }
     }

     public void sessionDestroyed(HttpSessionEvent se) {
          if(activeSessions > 0)
               activeSessions--;
     }

     public static int getActiveSessions() {
          return activeSessions;
     }

     public static myCountry getCountryClass(){
          return countries;
     }
}

Regards,
  Tomas Helgi
0
 
LVL 9

Author Comment

by:matthew016
ID: 17123894
Thank u Tomas but I can't see why I should destroy the data when no sessions are active,
also I can't see any code that "destroys" the data like u say.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

705 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

18 Experts available now in Live!

Get 1:1 Help Now