• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in sending resultset from servlet to jsp  RSS feed

 
Mahe Singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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:
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
ashish bassii
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahe Singh wrote:I will be using JSTL and EL.

Good plan!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!