• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in sending resultset from servlet to jsp

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am building an online quiz. I created a database , bean , controller and jsp. I connected database, wrote query , put resultset in arraylist of object and passed it to jsp. My program runs but arraylist size increases everytime and same question get displayed repeatedly . i cant find the error. Everytime i run the program arraylist size increases. I think it is adding same rows again ang again. here is my code for controller and jsp. please help.

servlet code:


jsp code:
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the list of questions is only the list for the current request, shouldn't it be a local variable in that servlet, created new for each request? I don't see where it's declared, which means it's probably an instance variable. Which is a Bad Thing. And shouldn't you be putting the list into request scope, instead of session scope?

Your JSP looks okay, except that since 2004 was a long time ago you shouldn't be using scriptlets. Not for new applications. You should be using JSTL and EL instead.
 
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your servlet, you should declare and initialize qblist inside the processRequest method.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
///ADD THIS LINE
List qblist = new ArrayList();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
con = ref.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select * from questions");


while (rs.next()){

Qbean qb = new Qbean();
qb.setQuestion(rs.getString("question"));
qb.setAns1(rs.getString("ans1"));
qb.setAns2(rs.getString("ans2"));
qb.setRightAns(rs.getString("rightans"));
qblist.add(qb);


}
try{

rs.close();
}catch(Exception ex){}
HttpSession session = request.getSession();
session.setAttribute("qblist", qblist);
RequestDispatcher rd = request.getRequestDispatcher("displayTest.jsp");
rd.forward(request, response);
}catch(Exception e){} finally {
out.close();
}
}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your using Java code in a JSP? In 2014?
 
Mahe Singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. issue resolved. I made my list as instance variable. I will be using JSTL and EL. first i have to learn it. This is sample program to see if all works. thanks again.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahe Singh wrote:I will be using JSTL and EL.


Good plan!
reply
    Bookmark Topic Watch Topic
  • New Topic