Display data from list on same jsp page
I have a jsp page in which i have few parameters for the user to select and on clicking submite
it shud display the data on the same jsp page from the servlet to which it goes
so these are the lines i used to go back to the jsp page
Servlet
-----------------
DBUtilLogin dbTrade= new DBUtilLogin();
List TradeDet=dbTrade.getTradesDate(paramMap);
request.setAttribute("TradeDet", TradeDet);
request.getRequestDispatcher("mainjsp.jsp").forwar d(request, response);
DBUtilLogin.java
---------------------
public List getTradesDate(Map<String, String> paramMap){
ResultSet rstd=null;
Statement sttraded=null;
if (con==null) init();
try{
String inves=paramMap.get("inves");
String frmdt=paramMap.get("from_date");
String todt=paramMap.get("to_date");
System.out.println("the investor id is retrieved "+inves);
StringBuilder sqltd=new StringBuilder("select * from trades where investor ='"+inves+"' and to_date(trd_date,'YYYYMMDD')>= to_date('"+frmdt+"', 'DD/MM/YYYY') and to_date(trd_date,'YYYYMMDD')<= to_date('"+todt+"', 'DD/MM/YYYY')");
sttraded=con.createStatement();
rstd=sttraded.executeQuery(sqltd.toString());
List tradeListd=new ArrayList();
Map obj;
while(rstd.next()){
obj=new HashMap();
obj.put("TRD_DATE", rstd.getString(1));
obj.put("MRKT", rstd.getString(2));
obj.put("QTY", rstd.getString(7));
obj.put("SEC", rstd.getString(9));
tradeListd.add(obj);
}
return tradeListd;
}catch (SQLException ex) {
//Logger.getLogger(DBUtilLogin.class.getName()).log( Level.SEVERE, null, ex);
ex.printStackTrace();
return null;
}
}
mainjsp.jsp
---------------
The data displays here if i give this
<table>
<c:forEach var="TradeDet" items="${TradeDet}">
<tr>
<td>"${TradeDet}</td>
</tr>
</c:forEach>
</table>
But its in an ugly format and not table format.
"[{SEC=fff, QTY=1000, MRKT=0, TRD_DATE=20110209}, {SEC=ttt, QTY=40000, MRKT=0, TRD_DATE=20120209} ...and so on it goes like this
i want it to display in a proper table format.
How do i display each row with proper data?
SEC QTY MRKT TRD_DATE
fff 1000 0 20110209
ttt 4000 0 20110209
PLease help!!
Re: Display data from list on same jsp page
Why don't you create table then?
Re: Display data from list on same jsp page
i am creating table..but how do i display each value in each column...i dont know to read teh data individually...
Re: Display data from list on same jsp page
Data is in the List? And why can't you just place each index of Data in each column?
Re: Display data from list on same jsp page
u mean to place each index of data in each column in the jsp page??cud u show me a piece of that code?
Re: Display data from list on same jsp page
Didn't you ever create a table before?
Place a loop and inside loop, draw columns, giving them the values for List's index.
Re: Display data from list on same jsp page
problem is solved.Thanks
this piece of code in the mainjsp.jsp solved it.
<%
List trad=(List)request.getAttribute("TradeDet");
if (trad!=null){
out.println("<table border=\"1\">");
for (int i=0;i<trad.size();i++)
{
Map obj=(Map) trad.get(i);
out.println("<tr>");
out.println("<td>"+obj.get("TRD_DATE")+"</td>");
out.println("<td>"+obj.get("MRKT")+"</td>");
out.println("<td>"+obj.get("SEC")+"</td>");
out.println("<td>"+obj.get("QTY")+"</td>");
out.println("</tr>");
}
out.println("</table>");
}
%>
The problem i was facing was i dint put the if null loop check for the list. so after logging in it used to give exception. Now after putting the if loop it checks if the list is empty or not. if empty it displays the remaining tags.else it displays data.
Re: Display data from list on same jsp page
if is not a loop but a conditional statement.