This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet filter question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet filter question" Watch "Servlet filter question" New topic
Author

Servlet filter question

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
I created a simple filter to set the vendor array list to all my requests since all my servlets will need this list. I wanted them the have a fresh list for every request. The thing is that i am doing a printout and i see the VendorDB.selectVendors() being called 30 times. Also i have some javascript on my links for rollovers and when i rollover them i see it going inot my filter then too. How come it is being called so may times? How can i prevent the db fom being called so many times? Or did i just code this wrong?

Filter:


Filter Mapping:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Your filter mapping is causing the filter to be called for every request. That will include images, script files and anything else.

If you only need the list for a servlet, map the filter to the servlet URL only.

Why do you need a "fresh list" on every request in the first place?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
well i have a left navigation that grabs the brand links from the db. i guess i could set it to the session and check if the object is null before querying the db. Is there a way to map an entire package instead of individual servlets? I would need to map all the servlets in my app.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

It's much easier if you use a Front Controller for all your actions. That way, there's only one servlet that ever gets activated, which then delegates to the specific command instance.

How volatile is this list? If you can avoid the filter entirely, that would simplify things.
[ November 01, 2008: Message edited by: Bear Bibeault ]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
well i could just set the request attribute and query the DB in all my servlets. i thought i could globalize it this way by putting the code in one central class. what about changing the url-pattern of all my servlets to something like /servlets/dispVendors. Where they all start with servlets and in the filter mapping do servlets/* ? still working on learning the front control pattern.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

You still haven't answered the question regarding why you need to recreate the list for every request.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
well every page needs this array list to create the links for the navigation. If i only set it to the session when the home page loads and that session expires, none of my pages will have that list. So i figured to make sure that list is always on my pages i should fill the array and set it to my session on every request. This way i am assured that the array is never empty.
[ November 02, 2008: Message edited by: John Schretz ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Is the list the same for every user?

If the list isn't different for every user, it's a simple matter to establish it once at app startup in application context using a context listener. If it's user-specific, it's also a simple matter to use the session and only load the list when it's not already there.

Does it not seem like incredible overkill to hit the DB at each and every request just to grab a list that doesn't often change?
[ November 02, 2008: Message edited by: Bear Bibeault ]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
you are absolutely right. the list is the same for all users. i will look into the context listener. if the list ever changed in the db and had to be updated would i just have to restart tomcat in order to refresh the list?

thanks
john
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
How does this look? would i need anything else?



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Originally posted by John Schretz:
you are absolutely right. the list is the same for all users. i will look into the context listener. if the list ever changed in the db and had to be updated would i just have to restart tomcat in order to refresh the list?


That'd be an easy way. But in most production environments, restarting the server is something that's avoided as much as possible so you could also have an admin servlet that you could hit (authenticated, please) to trigger a manual reload of the application-scoped variable whenever you update the database.

Or, you could even make the application-scoped variable "smart enough" to periodically check to see if it need to update its list -- but that's a more advanced topic.
Tamas Jano
Ranch Hand

Joined: Feb 21, 2007
Posts: 55
I think for your needs a static method that returns a static list of vendors and have a lookups service that gets the current list or a new one depending on your preference.

You can include a check into the getVendors() method for ex. if the static list has been initialized, if not retrieve the data from the db otherwise get the content of the already populated data. If you need to reloaded call the other method (getVendorsFromDb() for ex)

It works for me for countries, currencies etc.


And you will inject all the serices and db logic there somehow (Spring or else)
Note: that the vendors attribute is private so you can access it only via the getter.
[ November 03, 2008: Message edited by: Tamas Jano ]
Kevin Chen
Greenhorn

Joined: Nov 02, 2008
Posts: 5
set /* to <url-pattern> in the filter configuration that means will be all resource(*.css, *.js, *.jpg ... ...) pass .
[ November 03, 2008: Message edited by: best gembler ]

Free as in Freedom
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Originally posted by Tamas Jano:
I think for your needs a static method that returns a static list of vendors
That would be a poor way to accomplish this, and not a good OO practice to follow.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Originally posted by best gembler:
set /* to <url-pattern> in the filter configuration that means will be all resource(*.css, *.js, *.jpg ... ...) pass .
Yes, that has already been pointed out. Please don't simply repeat information that has already been posted.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
the listener works great. Thanks again. Is there a way to manually reload the context? Like how tomcat reloads the context when a class file is updated?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Yes. Look up the Tomcat Manager application.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
will do thanks again bear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet filter question