Retrive values from an arraylist and display in a jsp

D

Deepa

Hi,

I need help in jsp to display values of an arraylist in jsp.

There is a java file containing a method which is returning an
arraylist.

I want to display the record of arrayList in jsp in tabular form say 10
records.

can anyone help me on this.

Thanks in advance.
Deepa
 
H

hiwa

Deepa ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
Hi,

I need help in jsp to display values of an arraylist in jsp.

There is a java file containing a method which is returning an
arraylist.

I want to display the record of arrayList in jsp in tabular form say 10
records.

can anyone help me on this.

Thanks in advance.
Deepa
Is is like this? :
<TD><%= arrayList.get(n) %></TD>
 
D

Deepa

I have added in arrayList like this

retArrayList.add(rs.getString(5));
log.debug("First name of terminated emp : " +
rs.getString(5));
retArrayList.add(rs.getString(6));
log.debug("Last name of terminated emp : " +
rs.getString(6));

say if there are 7 records then arrayList returning its size as 14 (7
for firstname and 7 for lastname). but according to me its size shod be
7.
 
Joined
Mar 1, 2012
Messages
5
Reaction score
0
This code is in Struts how to collect listobjects from actionclass,helper,DAo and get that list in jsp using struts tags
objuserDatalist = objLoginHelper.getUserDataList();
request.setAttribute(USER_DATALIST, objuserDatalist);

-<---objUserDataList = objLoginDAO.getUserDataList();<---objEmpList = (ArrayList) session.createQuery("from EmployPOJO where empStatus='A'").list();
---------------------------------------------------------------------------
request.setAttribute(USER_DATALIST, objuserDatalist);

<logic:notEmpty name="userDataList">
<logic:iterate name="userDataList" id="userlist">
<td> <bean:write name="userlist" property="empId"/></td>
<td> <bean:write name="userlist" property="empName"/></td>
<td> <bean:write name="userlist" property="empDesg"/></td>
<td> <bean:write name="userlist" property="empComp"/></td>
<td> <bean:write name="userlist" property="empLoc"/></td>
<td> <bean:write name="userlist" property="empMob"/></td>
<td> <bean:write name="userlist" property="empPhone"/></td>
<td> <bean:write name="userlist" property="empMail"/></td>


</tr>
</logic:iterate>
</logic:notEmpty>
 
Last edited:
Joined
Mar 1, 2012
Messages
5
Reaction score
0
Retrive values from an arraylist and display in jsp

Hi,

I need help in jsp to display values of an arraylist in jsp.

There is a java file containing a method which is returning an
arraylist.

I want to display the record of arrayList in jsp in tabular form say 10
records.

can anyone help me on this.

Thanks in advance.
Deepa


from jdbc or hibernate return List (with db data either using jdbc or hibernate)and set that list into request is best practice (or session ) like request.setAttribute("objList", objuserDatalist); and get list in jsp by using JSTL or EL expressions(like $list)
  1. <C:IF test="${not empty names}">
  2. <C:FOREACH var="objList" items="${objList}">
  1. <C:OUT value="${objList.name}"></C:OUT>
  2. <C:OUT value="${objList.id}"></C:OUT>
  3. </C:FOREACH></C:IF>
 
Joined
Mar 1, 2012
Messages
5
Reaction score
0
hyperlink query string as parameter and collecting parameter process in java,struts

<td> <a href="viewEmploy.do?id=<bean:write name="userlist" property="empId"/>">viewEmployee</a></td>---->this if multiple records table data but when click on viewEmployee link it only has to display only one employ data depend on passed empId as parameter.in viewEmploy Action (becoz of viewEmploy.do)
if( request.getParameter("id")!=null)
{
empId=Integer.parseInt(request.getParameter("id"));
} EmployActionForm objEmpForm=(EmployActionForm)form;
UserDataBean objUserBean=objLoginHelper.retrieveEmployDetailsbyId(empId);-<---objEmpList = (ArrayList) session.createQuery("from EmployPOJO where empStatus='A' and empId = " + id).list();
objPojo = (EmployPOJO) objEmpList.get(0);
BeanUtils.copyProperties(objUserBean, objPojo);
return objUserBean;
objLoginHelper.popuateDataIntoActionForm(objUserBean,objEmpForm);
return mapping.findForward(SUCCESS);
<table border="1">
<tr>
<td>Employee Id :
</td>
<td>Employee Name :
</td>
<td>Employee Designation :
</td>
<td>Employee Company :
</td>
<td>Employee Location :
</td>
<td>Employee Mobile :
</td>
<td>Employee Email :
</td>
</tr>
<tr>
<td><bean:write name="EmployActionForm" property="empId"></bean:write>
</td>
<td><bean:write name="EmployActionForm" property="empName"></bean:write>
</td>
<td><bean:write name="EmployActionForm" property="empDesg"></bean:write>
</td>
<td><bean:write name="EmployActionForm" property="empComp"></bean:write>
</td>
<td><bean:write name="EmployActionForm" property="empLoc"></bean:write>
</td>
<td><bean:write name="EmployActionForm" property="empMob"></bean:write>
</td>
<td><bean:write name="EmployActionForm" property="empMail"></bean:write>
</td>
</tr>
</table>
 
Joined
Mar 1, 2012
Messages
5
Reaction score
0
display list in jsp from servlet and jdbc

from servlet and from dao list and to jsp display jsp.
in displaylist.java servlet inside processRequest or service or doget or dopost method
ArrayList aListUsersData = null;
String strPath = "home.jsp";
try {
DisplayDAO objDAO = new DisplayDAO();
aListUsersData = objDAO.retriveUsersList();
request.setAttribute("usersList", aListUsersData);
RequestDispatcher dispatcher = request.getRequestDispatcher(strPath);
dispatcher.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
------------------------------
DisplayDAO.java
ArrayList aListUsersData = null;
UserDataBean objUserBean = null;
try {
con = objDBCon.getConnection();
pstmtRef = con.prepareStatement(QueryConstants.getUserDataList);
rs = pstmtRef.executeQuery();
aListUsersData = new ArrayList();
while (rs.next()) {
objUserBean = new UserDataBean();
objUserBean.setEmpId(rs.getInt("emp_id"));
objUserBean.setEmpName(rs.getString("emp_name"));
objUserBean.setEmpDesg(rs.getString("emp_desg"));
objUserBean.setEmpComp(rs.getString("emp_comp"));
objUserBean.setEmpLoc(rs.getString("emp_loc"));
objUserBean.setEmpMob(rs.getString("emp_mob"));
objUserBean.setEmpPhone(rs.getString("emp_phone"));
objUserBean.setEmpMail(rs.getString("emp_email"));
aListUsersData.add(objUserBean);
}public static final String getUserDataList = "select * from employee where emp_status='A'";inside QueryConstants.java
home.jsp
<%
String userName = "";
ArrayList alistUsersList = null;
UserDataBean objUserBean = null;
if (session.getAttribute("user") != null) {
userName = (String) session.getAttribute("user");
} else {
response.sendRedirect("logout.jsp");
}
if (request.getAttribute("usersList") != null) {
alistUsersList = (ArrayList) request.getAttribute("usersList");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table align="center" width="80%"> <tr><td align="left">Welcome <%=userName%></td> <td align="right"><a href="logout">Logout</a></td></tr> </table>


<%
if (alistUsersList != null && !alistUsersList.isEmpty()) {
%>
<table border="1" width="80%" align="center">
<tr>
<td>Emp Id</td>
<td>Emp Name</td>
<td>Designation</td>
<td>Company</td>
<td>Location</td>
<td>View</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<%
for(int i=0; i< alistUsersList.size(); i++){
objUserBean = (UserDataBean) alistUsersList.get(i);
%>
<tr>
<td><%=objUserBean.getEmpId()%></td>
<td><%=objUserBean.getEmpName()%></td>
<td><%=objUserBean.getEmpDesg()%></td>
<td><%=objUserBean.getEmpComp() %></td>
<td><%=objUserBean.getEmpLoc() %></td>
 
Joined
Mar 1, 2012
Messages
5
Reaction score
0
USAGE OF QUERYSTRING parameters dynamically in HYPERLINK

in home.jsp after display list.
<td><a href="viewuser?id=<%=objUserBean.getEmpId()%>">View</a> </td>
String strId = "", strPath = "view.jsp";
try {
if (request.getParameter("id") != null) {
strId = request.getParameter("id");
}
UserDAO objDAO = new UserDAO();
UserDataBean objBean = null;
objBean = objDAO.retriveUserDataUser(strId);
request.setAttribute("userBean", objBean);
RequestDispatcher dispatcher = request.getRequestDispatcher(strPath);
dispatcher.forward(request, response);
---------------------------------------------------------------------------------
INSIDE view.jsp
<%if (request.getAttribute("userBean") != null) {
objBean = (UserDataBean) request.getAttribute("userBean"); }%>
<table align="center" width="80%"> <tr><td align="left">Welcome <%=userName%></td> <td align="right"><a href="logout">Logout</a></td></tr> </table>
<table width="80%" border="1">
<tr>
<td>Employee Id : </td>
<td><%=objBean.getEmpId()%></td>
</tr>
<tr>
<td>Employee Name : </td>
<td><%=objBean.getEmpName()%></td>
</tr>
<tr>
<td>Designation : </td>
<td><%=objBean.getEmpDesg()%></td>
</tr>
<tr>
<td>Company : </td>
<td><%=objBean.getEmpComp()%></td>
</tr>
<tr>
<td>Location : </td>
<td><%=objBean.getEmpLoc()%></td>
</tr>
<tr>
<td>Mobile : </td>
<td><%=objBean.getEmpMob()%></td>
</tr>
<tr>
<td>Phone : </td>
<td><%=objBean.getEmpPhone()%></td>
</tr>
<tr>
<td>E-Mail : </td>
<td><%=objBean.getEmpMail()%></td>
</tr>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top