问题描述
//我已经从 MySQL 中检索了一个结果并创建了 Array-list User .i 已经发送了这个//US er Array-list 并通过请求响应对象发送它.现在我需要在//s JSP 页面上显示它.//1.没有JSTL//2.用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
//表名是user_reg,它有四个字段id,username,password,email.//请举例说明.我需要显示一个jsp页面中的所有字段.但我不想//在 JSP 上做 jdbc 工作
//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);
}
}
//如何在JSP页面中获取这个
//how to get this in a JSP page
推荐答案
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();
使用 pojo 类访问它:
using pojo class access it :
user.getUsername();user.getPassword();
user.getUsername();user.getPassword();
%>
这篇关于如何在没有 JSTL 的情况下遍历 JSP 上的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!