我是JSP新手并尝试创建一个Web应用程序,我可以从mysql db中检索一个actor列表并在JSP页面中填充它。
我成功地从mysql数据库中检索了actor(firstname,lastname,actor_id)并填充了一个actor arraylist对象但是无法在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;
}
}
答案 0 :(得分:0)
我不确定无关的标签。试试这个:
<% ArrayList<Actor> acrs = (ArrayList<Actor>)request.getAttribute("actorslist"); %>
<c:forEach var="Actor" items="${acrs}">
FirstName:<c:out value="${Actor.getFirstname()}"/><br/>
</c:forEach>
特别是,删除较低的%>
(拼写错误?)