aspose file tools*
The moose likes JSP and the fly likes Displaying contents of arraylist in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying contents of arraylist in jsp" Watch "Displaying contents of arraylist in jsp" New topic
Author

Displaying contents of arraylist in jsp

hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
Hello guys,

Could anyone give me solution to the following query please....

I have a class....


package data;

public class Details {

long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
String userName;
String compName;
String Desg;
String emailId;
String lang;
String gender;
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCompName() {
return compName;
}
public void setCompName(String compName) {
this.compName = compName;
}
public String getDesg() {
return Desg;
}
public void setDesg(String desg) {
Desg = desg;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}

}


following servlet


package servletClasses;

import java.io.IOException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ViewDetails;

import data.Details;

/**
* Servlet implementation class View_details
*/
public class View_details extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public View_details() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

String page = "FormData.jsp";
ViewDetails datas = new ViewDetails();

List<Details> newlist = datas.selectData();

request.setAttribute("FormDatas", newlist);

RequestDispatcher disp = request.getRequestDispatcher(page);

if(disp != null){
disp.forward(request,response);
}


}

}


Now the question is, how do I display the data present in list of Details class in jsp table....

I managed to do the following in my jsp page... can anyone correct me...

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*, data.Details"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="javascript/onClickRad.js">
</script>
</head>
<body>
<form action="" name="frm" method="post">
<div style="display: inline">
Update Existing Record
<input type="radio" id="radA" value="UpdateExistingRecord" name="updRad" onclick="return disable(this);"/>  
Insert New Record
<input type="radio" id="radB" value="InsertNewRecord" checked="checked" name="updRad" onclick="return disable(this);" />  
Delete Record
<input type="radio" id="radC" value="DeleteRecord" name="updRad" onclick="return disable(this);"/><br />
</div><br /><br />
</form>
<form action="" method="post" name="frmbtn">
<label id="lblName">User Name:</label>
  
<input id="txtName" name="txt" type="text" />
  
<label id="lblEmail">Email ID:</label>
  
<input id="txtEmail" type="text" /><br />
<label id="lblCmp">Company:</label>
  
<input id="txtCmp" type="text" />
  
<label id="lblDesg">Designation:</label>
  
<input id="txtDesg" type="text" /><br /><br />

<div>
<input id="btnIns" name="btnInsert" type="submit" value="Insert" onclick="return validate(this);" />
  
<input id="btnUpd" name="btnUpdate" type="submit" value="Update" disabled="disabled" onclick="return validate(this);" />
  
<input id="btnDlt" name="btnDelete" type="submit" value="Delete" disabled="disabled" onclick="return validate(this);" />
  
</div>
</form><br />
<form action="viewdetails.do" method="post" name="frmView">
<input id="btnVw" name="btnView" type="submit" value="View Details" disabled="disabled" />
</form>
<table border="1" width="500">
<tr>
<td width="119"><b>ID</b></td>
<td width="234"><b>USER NAME</b></td>
<td width="234"><b>EMAIL_ID</b></td>
<td width="234"><b>DESIGNATION</b></td>
<td width="234"><b>COMPANY NAME</b></td>
<td width="234"><b>LANGUAGES</b></td>
<td width="234"><b>GENDER</b></td>

</tr>
<%Iterator<Details> itr; %>
<%List<Details> list = (List<Details>)request.getAttribute("FormDatas");
if(list != null){

for(itr=list.iterator(); itr.hasNext();){
%>
<tr>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>


</tr>



<%}} %>

</table>
</body>
</html>


Thanks in advance...
Hemanth.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

You should be using the JSTL <c:forEach> tag to iterate over the List, not Java scriptlets which have been discredited for over 10 years now.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
Hi Bear Bibeault,

Thanks for timely reply...

can you post the snippet as to how its done please...


Thank you,
Hemanth.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

How to use the JSTL
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying contents of arraylist in jsp