Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database Access Repeated Each Time

 
Shajid Johnny
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic