Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning ArrayList from servlet to jsp  RSS feed

 
Aditya Dhekney
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have been trying to do what i thought would be a very simple thing...perform a search based on some criteria, and return the query result back to be displayed on a jsp page.
I have a jsp page where some dropdown lists are populated on entry to the page. This works fine. I want to (eventually!) perform the search based on what is selected in these dropdowns. When i click the search button, i invoke a servlet called "SearchInventory.java" which gets the result. I then store this result in an ArrayList object. I then set a bean property (bean name->SearchBean, property->inventory) from within the servlet, and redirect back to the search page. This works if the query only gets back one column.
Unfortunately, when i ask for more than one column in the query, i get a "java.lang.NullPointerException" error.
Here is my code for all segments:
--------------------------------------------------------
JSP:
<%@ page language="java" import="java.util.ArrayList,java.lang.*,java.sql.*,javax.sql.*,PopLists.PopInvLists,beans.SearchBean"%>

<%
SearchBean sbean=new SearchBean();
ArrayList myAry=sbean.getInventory();
int size=myAry.size();
%>
.
.
.
<%
for(int i=0;i<size;i++){
String pname=(String)myAry.get(i);
%>
...
<%}%>
-------------------------------------------------------
Servlet:
String QueryStr="select ProdName,ManufName from products";// where ProdID="+ProdID;
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(QueryStr);
while(rs.next()){
rowAry.add(rs.getString(1));
rowAry.add(rs.getString(2));
rowSetAry.add(rowAry.clone());
}
sbean.setInventory(rowSetAry);
req.getSession(true).setAttribute("s_resbean",sbean);
.
.
res.sendRedirect("/DBTest/SearchInventory.jsp");
-----------------------------------------------------------------------------------------------------
In the servlet, i have also tried the following method of redirecting back to jsp page:
String url="/SearchInventory.jsp";
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(req,res);
...this gives me a java.lang.ClassCastException
-------------------------------------------------------------------------------------------------------
here is my "bean" class:
public class SearchBean extends HttpServlet{
private int searchFlag=0;
private static ArrayList inventory;
...
public static ArrayList getInventory(){
return inventory;
}
public void setInventory(ArrayList rs){
this.inventory=rs;
}
---------------------------------------------------------------------------------------------------------
When i perform a query that selects just one column, and doesn't use any sort of search fields for selecting, it does seem to work. for eg, : "select ProdName from products" as the query will return the product names successfully.
Now, i realize what i'm doing is not correct....however, i don't know what the correct way of doing this is. Can someone please please show me how i should be going about doing all of this? It really shouldn't be difficult, but I just can't seem to get it to work.
Thanks in advance,
Aditya
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37261
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya,
It looks like you are storing the data in a 2D arraylist, but retrieving it from a 1 dimensional arraylist. You may want to store the data in a 1D arraylist of value objects (getters and setters for each field), so the data makes sense. It will also be easier to track down problems and maintain.
 
Aditya Dhekney
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Aditya,
You may want to store the data in a 1D arraylist of value objects (getters and setters for each field), so the data makes sense. It will also be easier to track down problems and maintain.

How do i go about doing this? Can you please give me some code examples?
Thanks in advance,
Aditya
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also wrap your results up in an object and then set the object on the object to the collection.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37261
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boson explained what I was trying to get at:

This assumes you have a Stuff class with two fields and the needed getters and setters.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!