• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Servlet filter question

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still haven't answered the question regarding why you need to recreate the list for every request.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does this look? would i need anything else?



 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Look up the Tomcat Manager application.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will do thanks again bear
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic