我有一个servlet,它从数据库中读取数据并填充列表。我想在jsp文件中显示此列表,但我无法显示行。有人可以说明我做错了吗?这些是文件:
ListCars.java(servlet)
package servlets;
[imports]
@WebServlet(name="ListCars", loadOnStartup = 1,urlPatterns={"/ListCars"})
public class ListCars extends HttpServlet {
@EJB
private CarsFacade carsFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession(true);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
List dbData = carsFacade.findAll();
request.setAttribute("data",dbData);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
String url = "/index.jsp";
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
// use RequestDispatcher to forward request internally
String url = "/index.jsp";
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
的index.jsp:
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="design.css">
<title>Car Hire</title>
</head>
<body>
<div id="main">
<div id="header">
<div id="widgetBar">
<div class="headerWidget">
<a href="#">
Login
</a>
</div>
<div class="headerWidget"></div>
<div class="headerWidget">
<a href="#">
Register
</a>
</div>
</div>
</div>
<div id="indexMidColumn">
<div id="welcomeText">
<p>Car Hire</p>
</div>
<table border="1" width="303">
<tr>
<td width="119"><b>ID</b></td>
</tr>
<c:forEach items='${data}' var='CarsAv' varStatus="loop">
<tr><td>${CarsAv.Brand}</td></tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>ListCars</servlet-name>
<servlet-class>servlets.ListCars</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ListCars</servlet-name>
<url-pattern>/ListCars</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>