我是 JSP 的新手,正在尝试创建一个 Web 应用程序,我可以在其中从 mysql db 检索演员列表并将其填充到 JSP 页面中。
我成功地能够从 mysql 数据库中检索演员(名字、姓氏、演员 ID)并填充到演员数组列表对象中,但无法在 JSP 页面中填充相同的对象,
这是我的代码,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.dbconnect.Actor,java.util.*"%>
<!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>All the actors from SAKILA database</title>
</head>
<body>
Im in get actors.jspp
<%ArrayList<Actor> acrs = (ArrayList<Actor>)request.getAttribute("actorslist");%>
<c:forEach var="Actor" items="${acrs}">
FirstName:<c:out value="${Actor.getFirstname()}"></c:out>
</c:forEach>
%>
</body>
</html>
Servlet code
package com.dbconnect;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class actorservlet
*/
public class actorservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public actorservlet() {
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)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Im in actorsservlet");
Actors acrs = new Actors();
ArrayList<Actor> actorslist = null;
try {
actorslist = acrs.getactors();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Actor ac:actorslist)
System.out.println(ac.firstname);
request.setAttribute("actorslist", actorslist);
RequestDispatcher dispatcher = request.getRequestDispatcher("getactors.jsp");
//response.sendRedirect("getactors.jsp");
dispatcher.forward(request, response);
return;
}
}
Actors.java
package com.dbconnect;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Actors {
List<Actor> ls = new ArrayList<Actor>();
DbContext db = null;
Actors(){
try {
this.db = new DbContext();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<Actor> getactors() throws ClassNotFoundException,
SQLException {
ResultSet rs = this.db.getrows();
populateactorlist(rs);
return (ArrayList<Actor>) ls;
}
public void populateactorlist(ResultSet rs) throws SQLException {
while (rs.next()) {
Actor rc = new Actor();
rc.setActor_id(rs.getInt("actor_id"));
rc.setFirstname(rs.getString("first_name"));
rc.setLastname(rs.getString("last_name"));
ls.add(rc);
}
}
}
}
Actor.java
package com.dbconnect;
public class Actor {
Integer actor_id = null;
String firstname = null;
String lastname = null;
public Integer getActor_id() {
return actor_id;
}
public void setActor_id(Integer actor_id) {
this.actor_id = actor_id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}