Java 为什么我不能从servlet接收列表?
JSP页面。我想从MainServlet接收ArrayList。但Servlet似乎根本没有响应,因为它一直发送空值。我检查了数据库层,但它工作正常Java 为什么我不能从servlet接收列表?,java,Java,JSP页面。我想从MainServlet接收ArrayList。但Servlet似乎根本没有响应,因为它一直发送空值。我检查了数据库层,但它工作正常 <% HttpSession mainSession = request.getSession(false); User user = (User)mainSession.getAttribute("objectUser"); %> <div class="head
<%
HttpSession mainSession = request.getSession(false);
User user = (User)mainSession.getAttribute("objectUser");
%>
<div class="header">
<h1><c:out value="${sessionScope.objectUser.username}"/></h1>
</div>
<%
ArrayList<UserPost> userPosts = (ArrayList<UserPost>)request.getAttribute("ArrayListPost");%>
<div class="main">
<c:forEach items="${requestScope.ArrayListPost}" var="obj">
<div class="block">
<div class="blockText">
<h1><c:out value="${obj.postHeading}"/></h1>
<p><c:out value="${obj.postBody}"/></p>
</div>
</div>
</c:forEach>
我的servlet:
public class MainServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserPostDao userPostDao = new UserPostDao();
HttpSession mainSession = request.getSession(false);
User user = (User)mainSession.getAttribute("objectUser");
try {
ArrayList<UserPost> userPosts = userPostDao.getAllByUsername(user.getUsername());
RequestDispatcher requestDispatcher = request.getRequestDispatcher("main.jsp");
request.setAttribute("ArrayListPost", userPosts);
requestDispatcher.forward(request, response);
} catch (SQLException throwable) {
throwable.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public类MainServlet扩展了HttpServlet{
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
UserPostDao UserPostDao=新的UserPostDao();
HttpSession mainSession=request.getSession(false);
User=(User)mainSession.getAttribute(“objectUser”);
试一试{
ArrayList userPosts=userPostDao.getAllByUsername(user.getUsername());
RequestDispatcher=request.getRequestDispatcher(“main.jsp”);
setAttribute(“ArrayListPost”,userPosts);
转发(请求、响应);
}捕获(SQLException可丢弃){
printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
谢谢!:)我已经检查了你的代码。一切都很好。只有调试可以帮助)你可以将对象写入页面,然后查看对象中的。