| 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
|
 |
 |
|
|
subject: Database Access Repeated Each Time
|
|
|