Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jsp填充对象数组_Java_Jsp - Fatal编程技术网

Java jsp填充对象数组

Java jsp填充对象数组,java,jsp,Java,Jsp,我是JSP新手,正在尝试创建一个web应用程序,在这个应用程序中,我可以从mysql数据库检索参与者列表,并将其填充到JSP页面中 我成功地从mysql数据库中检索到actor(firstname、lastname、actor_id),并填充到actors arraylist对象中,但无法在JSP页面中填充相同的对象 这是我的密码 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page

我是JSP新手,正在尝试创建一个web应用程序,在这个应用程序中,我可以从mysql数据库检索参与者列表,并将其填充到JSP页面中

我成功地从mysql数据库中检索到actor(firstname、lastname、actor_id),并填充到actors 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;
    }

}

来自SAKILA数据库的所有参与者
我在get actors.jspp中
名字:
%>
Servlet代码
包com.dbconnect;
导入java.io.IOException;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入javax.servlet.RequestDispatcher;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
/**
*Servlet实现类actorservlet
*/
公共类actorservlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*@参见HttpServlet#HttpServlet()
*/
公共actorservlet(){
超级();
//TODO自动生成的构造函数存根
}
/**
*@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
}
/**
*@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
System.out.println(“Im-in-actorsservlet”);
参与者acrs=新参与者();
ArrayList actorslist=null;
试一试{
actorslist=acrs.getactors();
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
for(演员ac:actorslist)
System.out.println(ac.firstname);
setAttribute(“ActorList”,ActorList);
RequestDispatcher=request.getRequestDispatcher(“getactors.jsp”);
//sendRedirect(“getactors.jsp”);
转发(请求、响应);
返回;
}
}
Actors.java
包com.dbconnect;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入java.util.List;
公营演员{
List ls=新的ArrayList();
DbContext db=null;
演员(){
试一试{
this.db=new DbContext();
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
public ArrayList getactors()抛出ClassNotFoundException,
SQLException{
ResultSet rs=this.db.getrows();
民众名单(rs);
返回(ArrayList)ls;
}
公共void populateactorlist(结果集rs)引发SQLException{
while(rs.next()){
Actor rc=新Actor();
rc.setActor_id(rs.getInt(“actor_id”);
rc.setFirstname(rs.getString(“first_name”);
rc.setLastname(rs.getString(“姓氏”);
ls.加入(rc);;
}
}
}
}
Actor.java
包com.dbconnect;
公共级演员{
整数actor_id=null;
字符串firstname=null;
字符串lastname=null;
公共整数getActor_id(){
返回actor_id;
}
public void setActor_id(整数actor_id){
this.actor\u id=actor\u id;
}
公共字符串getFirstname(){
返回名字;
}
public void setFirstname(字符串firstname){
this.firstname=firstname;
}
公共字符串getLastname(){
返回姓氏;
}
public void setLastname(字符串lastname){
this.lastname=lastname;
}
}

我不确定是否有多余的标签。试试这个:

<% ArrayList<Actor> acrs =  (ArrayList<Actor>)request.getAttribute("actorslist"); %>
<c:forEach var="Actor"  items="${acrs}">
    FirstName:<c:out value="${Actor.getFirstname()}"/><br/>
</c:forEach>

名字:

特别是,删除较低的
%>
(输入错误?

我得到了这个错误--org.apache.jasper.JasperException:/getactors.jsp(15,11)当未指定默认名称空间时,函数getFirstname必须与前缀一起使用为什么同时使用scriptlet和jstl?坚持使用jstl。