aspose file tools*
The moose likes JDBC 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
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database Access Repeated Each Time" Watch "Database Access Repeated Each Time" New topic
Author

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: 92

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.

http://serfj.sourceforge.net - Simplest Ever REST Framework for Java
Srinivas Kollaparthi
Greenhorn

Joined: Jan 19, 2008
Posts: 11
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Database Access Repeated Each Time