0
我不知道爲什麼我的servlet無法正常工作。我的代碼波紋管:Servlets,JPA,JSP - Servlet無法正常工作
userOrdersServlet.java
@WebServlet("/userOrders")
public class userOrdersServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
int userId = (int) session.getAttribute("userId");
UserDAO daoUser = (UserDAO) request.getAttribute("userDao");
User user = daoUser.getUser(userId);
List<Books> booksOrderedByUser = user.getBooks();
request.setAttribute("booksOrderedByUser", booksOrderedByUser);
request.getRequestDispatcher("/userOrders.jsp").forward(request, response);
}
}
userDAO.java
public User getUser(int id) {
this.em.clear();
return this.em.find(User.class, id);
}
userOrders.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Library Online</title>
</head>
<body>
<div id="container">
<%@include file="templateTop.jsp" %>
<div class="main"><table border="1">
<h2>Your orders</h2>
<tr>
<td>Title</td>
<td>Author</td>
<td>Description</td>
<td>Category</td>
<td>Year</td>
</tr>
<c:forEach var="booksOrdered" items="${booksOrderedByUser}">
<tr>
<td>${booksOrdered.title}</td>
<td>${booksOrdered.author}</td>
<td>${booksOrdered.description}</td>
<td>${booksOrdered.category}</td>
<td>${booksOrdered.year}</td>
</tr>
</c:forEach>
</table>
</div>
</div>
<div style="clear:both"></div>
<br>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RegisterLoginJPA</display-name>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>filters.sessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>/loginSuccess.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
因此,我使用userOrdersServlet.java
中的代碼,並使用foreach循環顯示數據,並在另一個servlet中顯示System.out.println()
,它工作正常。但是當我去userOrders.jsp
它不顯示任何數據,我做錯了什麼?
安置自己的web.xml和你userOrder.jsp的休息吧。你還面臨什麼錯誤? – Perdomoff
我沒有注意到任何錯誤,每個其他的servlet運行良好,我把簡單的重定向到doGet方法在這個servlet中,仍然無法正常工作。 – Valery
你想完成什麼?你的預期產出是多少? – Perdomoff