'How to iterate through an array list on a JSP without JSTL
//I have retrieved a Result from MySQL and created and Array-list User .i have sent this //US er Array-list in and sent it through request Response Object. Now i need to display iton //s JSP page. //1.Without JSTL //2.With JSTL
//The Name of table is user_reg it has four fields id,username,password,email. //please do explain with example. i need to dispay all fields in a jsp page. but i dont want //to do the jdbc work on the JSP
package kinder.dto;
public class User {
private String id;
private String userName;
private String saltedkey;
private String emailId;
private String legalName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLegalName() {
return legalName;
}
public void setLegalName(String legalName) {
this.legalName = legalName;
}
public String getEmailId() {
return email;
}enter code here`
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return saltedkey;
}
public void setPassword(String password) {
this.saltedkey = password;
}
}
//dto
package kinder.dto;
public class User {
private String id;
private String userName;
private String saltedkey;
private String emailId;
private String legalName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLegalName() {
return legalName;
}
public void setLegalName(String legalName) {
this.legalName = legalName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return saltedkey;
}
public void setPassword(String password) {
this.saltedkey = password;
}
}
//servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<User> users = UserDAO.list();
request.setAttribute("users", users); // Will be available as ${products} in JSP
request.getRequestDispatcher("loginSuccess.jsp").forward(request, response);
} catch (SQLException | ClassNotFoundException e) {
throw new ServletException("Cannot obtain products from DB", e);
}
}
//how to get this in a JSP page
Solution 1:[1]
get some like this :
on jsp psge :
<% ArrayList<user> userList=(ArrayList<user>) request.getAttribute("user");
Iterator<user> iter = userList.iterator();
while(iter.hasNext()){
user user = iter.next();
using pojo class access it :
user.getUsername(); user.getPassword();
%>
Solution 2:[2]
With JSTL
You need to create POJO class
class UserReg {
private Integer id;
private String userName;
private String password;
private String email;
//getters and setters
}
Servlet
Need to change list population logic
//code.....
ResultSet rs = ...;
ArrayList<UserReg> usersList = new ArrayList<UserReg>();
while(rs.next()) {
//here create new object of UserReg for each row
UserReg user = new UserReg();
user.setId(rs.getInt(0));
// do it for userName, password, email
.....
.....
....
// last add to list
usersList.add(user);
}
//set list in request scope and forward request to JSP
request.setAttribute("usersList",usersList);
JSP
<c:forEach var="user" items="usersList">
<c:out value="${user.id}" />
<c:out value="${user.userName}" />
<c:out value="${user.password}" />
<c:out value="${user.email}" />
</c:forEach>
Solution 3:[3]
<%@page import="java.util.ArrayList"%>
<%@page import="kinder.dto.User"%>
...
...
<%
ArrayList<User> users = (ArrayList<User>) request.getAttribute("users");
for (User : users) {
%>
<div><%= user.getId() %></div>
<%
}
%>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dnyaneshwar |
Solution 2 | Aniket Kulkarni |
Solution 3 |