It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes Database Access Repeated Each Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Database Access Repeated Each Time" Watch "Database Access Repeated Each Time" New topic

Database Access Repeated Each Time

Shajid Johnny
Ranch Hand

Joined: May 15, 2010
Posts: 34
Hello dear members,
I want to display some sub categories when i click on a category. I sent the category ID using query string from jsp to servlet. in that servlet i wrote JDBC codes, executed query and saved all the subcategories in a List. Later I dispatched that to a JSP to show them. my servlet code is following:

The sub category list is iterated & displayed in the jsp page.

But the problem is- the Sub categories are repeated and appended bellow each time i request.

what I guess is the servlet's jdbc code is accessed every time.

Could you please elaborate on this??

Thanks in advance
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 93

Servlets are initialized once, so your method doGet is appending sub-categories to 'subCategoryList' each request.

Initialize subCategoryList to nothing or null, then in doGet you must create a new List each time. Or you could clear the list before adding sub-categories.
Srinivas Kollaparthi

Joined: Jan 19, 2008
Posts: 12
Never use request specific objects as instance varibles in Servlet class.
List subCategoryList = new ArrayList();

Move the above statement into doGet method.

Shajid Johnny
Ranch Hand

Joined: May 15, 2010
Posts: 34
Thanks a lot guys! The problem is solved
as you suggested- i moved the List declaration inside the method & it did the job.

Thanks again for being so helpful
I agree. Here's the link:
subject: Database Access Repeated Each Time
jQuery in Action, 3rd edition