从Servlet打印JSP中的列表数据

时间:2015-12-17 18:43:29

标签: java jsp servlets arraylist

我正在尝试从Servlet向JSP发送一个列表,我正在使用JSTL标签来打印列表数据。当我尝试在控制台中打印时,该列表实际上包含数据和显示。但是,当我运行JSP页面时,我没有显示数据,而是打印出来。

                  The Cars details are: 
               Year Model Price KM City Fuel 
${record} ${record.model} ${record.price} ${record.kms} ${record.city} ${record.fuel} 
                             Go Home 

ViewServlet.java

package com.student.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.student.bean.Car;
import com.student.dao.CarDaoImpl;

/**
 * Servlet implementation class ViewServlet
 */
public class ViewServlet extends HttpServlet {
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String page = request.getParameter("page");
    System.out.println("In servlet");
    RequestDispatcher rd = null;

    String id = request.getParameter("year");
    String year = null;
    if (id != null)
        year = (id);
    System.out.println("In servlet");

    System.out.println(year);
    Car Car = new CarDaoImpl().viewCar(year);
    ArrayList<Car> list = new CarDaoImpl().viewCar2(year);

    System.out.println(list);
    int i = 0;
    for (Car iter : new CarDaoImpl().viewCar2(year)) {
        System.out.print(i++);

        System.out.print(" Model is: " + iter.getModel());
        System.out.print(" Price is: " + iter.getPrice());
        System.out.print(" Year is: " + iter.getYear());
        System.out.print(" City  is: " + iter.getCity());
        System.out.print(" Fuel is: " + iter.getFuel());
        System.out.println(" Kms is: " + iter.getKms());

    }

    CarDaoImpl userDAO = new CarDaoImpl();

    List<Car> users = new ArrayList<Car>();
    users = userDAO.viewCar2(year);

    for (Car iter1 : userDAO.viewCar2(year)) {
        System.out.print(i++);
        System.out.print(" Model is: " + iter1.getModel());
        System.out.print(" Price is: " + iter1.getPrice());
        System.out.print(" Year is: " + iter1.getYear());
        System.out.print(" City  is: " + iter1.getCity());
        System.out.print(" Fuel is: " + iter1.getFuel());
        System.out.println(" Kms is: " + iter1.getKms());

    }

    request.setAttribute("list", users);
    RequestDispatcher requestDispatcher = request.getRequestDispatcher("display.jsp");
    requestDispatcher.forward(request, response);

}

}

display.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Car display details</title>
</head>
<body>
<center>
    <th>The Cars details are:
    <th>
        <table>
            <tr>
                <th>Year</th>
                <th>Model</th>
                <th>Price</th>
                <th>KM</th>
                <th>City</th>
                <th>Fuel</th>

            </tr>


            <c:forEach items="${list}" var="record">
                <tr>
                    <td>${record.year }</td>
                    <td>${record.model }</td>
                    <td>${record.price }</td>
                    <td>${record.kms}</td>
                    <td>${record.city }</td>
                    <td>${record.fuel }</td>
                </tr>
            </c:forEach>
        </table> <a href="index.jsp">Go Home</a>
</center>
</body>
</html>

0 个答案:

没有答案