Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

iterating arraylist in jsp page  RSS feed

 
suraj savaratkar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

can somebody tell me how to iterate arraly list in jsp from action class.
i listed bellow as : in .java file


on jsp page


but it not working, who helped will appreciated.....
Thanks...
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not working is not helpful. What specific problem are you having? Please copy and paste the stack trace.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only is it not clear what is not working, your code seems to make not much sense.

You have a Java method that goes through a lot of steps, and then just returns the hard-coded variable "areaName". What's the purpose of all that?

Then in the code, you try to reference a scoped variable named "arrayList" (surely you can come up with a more descriptive name?) but nowhere in the code you posted does it show how that scoped variable gets set. Then in the loop, you create an iterator named theater that you don't use, but you reference another scoped variable named areaName that has never been declared.

It's not clear at all what you are trying to accomplish in the first place.

P.S. You also do not seem to be closing your database resources!
 
suraj savaratkar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Not only is it not clear what is not working, your code seems to make not much sense.

You have a Java method that goes through a lot of steps, and then just returns the hard-coded variable "areaName". What's the purpose of all that?

Then in the code, you try to reference a scoped variable named "arrayList" (surely you can come up with a more descriptive name?) but nowhere in the code you posted does it show how that scoped variable gets set. Then in the loop, you create an iterator named theater that you don't use, but you reference another scoped variable named areaName that has never been declared.

It's not clear at all what you are trying to accomplish in the first place.

P.S. You also do not seem to be closing your database resources!


You are right;
but here is complete java code;

package com.suraj;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.suraj.pojo.ShowTheaterPOJO;

import dbconnection.DBManager;

public class ShowTheaterAction extends ActionSupport implements SessionAware {

private String qurString = "select * from tbl_area";
private Connection connection;
private DBManager dbManager = new DBManager();
private ArrayList<ShowTheaterPOJO> arrayList = new ArrayList<ShowTheaterPOJO>();
private Statement statement;
private ResultSet resultSet;
private ShowTheaterPOJO showtheaterPojo;
private Map<String, Object> sessionMap = new Hashtable<String, Object>();
private Iterator iterator;

public String getAreaName() {
try {
connection = dbManager.getDBConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(qurString);
while (resultSet.next()) {
showtheaterPojo = new ShowTheaterPOJO();
String areaid = resultSet.getString("areaid");
String areaname = resultSet.getString("areaname");
String adminid = resultSet.getString("adminid");

showtheaterPojo.setAreaid(areaid);
showtheaterPojo.setAreaname(areaname);
showtheaterPojo.setAdminid(areaid);

System.out.println();
arrayList.add(showtheaterPojo);
sessionMap.put("showtheaterPojo", showtheaterPojo);
}
iterator = arrayList.iterator();
while (iterator.hasNext()) {
showtheaterPojo = (ShowTheaterPOJO) iterator.next();
System.out.print(showtheaterPojo.getAdminid());
System.out.print(showtheaterPojo.getAreaid());
System.out.print(showtheaterPojo.getAreaname());
System.out.println();
}

iterator = sessionMap.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println("key:" + key);
System.out.println("value:" + sessionMap.get(key));
}



} catch (SQLException e) {
e.printStackTrace();
e.getCause();
e.getMessage();
System.out.println(e);
}

return "areaName";
}

@Override
public void setSession(Map<String, Object> arg0) {
this.sessionMap = arg0;

}

}

I am trying to get the value from database and put into the arraylist and iterate in jsp page..
how to iterate that arraylist in the jsp page..

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already been asked to use code tags. I will not look at code that is not posted in code tags.

Also, you haven't answered the questions that have been posed.
 
suraj savaratkar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj savaratkar wrote:
Bear Bibeault wrote:Not only is it not clear what is not working, your code seems to make not much sense.

You have a Java method that goes through a lot of steps, and then just returns the hard-coded variable "areaName". What's the purpose of all that?

Then in the code, you try to reference a scoped variable named "arrayList" (surely you can come up with a more descriptive name?) but nowhere in the code you posted does it show how that scoped variable gets set. Then in the loop, you create an iterator named theater that you don't use, but you reference another scoped variable named areaName that has never been declared.

It's not clear at all what you are trying to accomplish in the first place.

P.S. You also do not seem to be closing your database resources!


You are right;
but here is complete java code;

 
suraj savaratkar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj savaratkar wrote:
suraj savaratkar wrote:
Bear Bibeault wrote:Not only is it not clear what is not working, your code seems to make not much sense.

You have a Java method that goes through a lot of steps, and then just returns the hard-coded variable "areaName". What's the purpose of all that?

Then in the code, you try to reference a scoped variable named "arrayList" (surely you can come up with a more descriptive name?) but nowhere in the code you posted does it show how that scoped variable gets set. Then in the loop, you create an iterator named theater that you don't use, but you reference another scoped variable named areaName that has never been declared.

It's not clear at all what you are trying to accomplish in the first place.

P.S. You also do not seem to be closing your database resources!


You are right;
but here is complete java code;



I am trying to get the value from database and put into the arraylist and iterate in jsp page..
how to iterate that arraylist in the jsp page..




and jsp file ids :

[code=java]

<%@page import="dbconnection.DBManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Date"%>

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>

<link href="css_files/selectarea_dropdown.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function getChanged(changevalue)
{
var selectedValue = document.getElementById('selectedArea').value;
alert("you select " + selectedValue);
}

function date_time(id)
{
date = new Date;
year = date.getFullYear();
month = date.getMonth();
months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
d = date.getDate();
day = date.getDay();
days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
h = date.getHours();
if(h<10)
{
h = "0"+h;
}
m = date.getMinutes();
if(m<10)
{
m = "0"+m;
}
s = date.getSeconds();
if(s<10)
{
s = "0"+s;
}
result = ''+days[day]+' '+months[month]+' '+d+' '+year+' '+h+':'+m+':'+s;
document.getElementById(id).innerHTML = result;
setTimeout('date_time("'+id+'");','1000');
return true;
}

</script>
</head>
<body>
<div class="select-option"> <select id="selectedArea" onchange="getChanged(this);"> <option>--- Select Area ---</option> <c:forEach var="theater" items="${arrayList}"> <option> <c:out value="${areaname}"></c:out> </option> </c:forEach> <% int hour,second,min; Date date = new Date(); String todate = date.toString(); hour = date.getHours(); second = date.getSeconds(); min = date.getMinutes(); String time = hour + " : " + min + " : "+second; %> </select> </div> <div style="font-family: Lucida Handwriting, Segoe Script, Snap ITC;" align="right"> <span id="date_time"></span> <script type="text/javascript">window.onload = date_time('date_time');</script> </div>

</body>
</html>



[code]


Im trying to do fetch the database value from table fields and stored in ArrayList and iterate in the jsp page.
I trying as i understood. But its not enough. Please help me
al i posted this code in code tag..

Thanks for suggestion....
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try to go easy on our eyes by putting your code between code tags like,

[code=java]
// your code
[/code]
 
suraj savaratkar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Ahsan Bagwan]Please try to go easy on our eyes by putting your code between code tags like,

[code=java]
// your code
[/code] [/quote]

[quote=suraj savaratkar]
thanks for the suggestion, i will be take care of the code tag onwards

can you tell that how to iterate arraylist from action class to jsp jage through OGNL expression; in
[/quote]
[code=java]
<s:iterate value=""> </s:iterate>
[/code]

Thanks ....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!