This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
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: 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. - Simplest Ever REST Framework for Java
Srinivas Kollaparthi

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
I agree. Here's the link:
subject: Database Access Repeated Each Time
Similar Threads
java .lang .Number Format Error
Problem in getting the data from DB using servlets
Setting the attribute
Difficulty in Updating database
error message from servlet to jsp