I print the list directly in the servlet using a printing device and print the list.
When I try to insert jsp, the list does not print whether I use JSTL or scriptlets.
I tried to test the JSTL and scriptlet if the object is null and it turns out that it is!
Why is this happening and how can I fix it?
Servlet code that works
for (Artist artist:artists){ resp.getWriter().println(artist.getName()); }
The code for the servlet that places the object in the request
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { ApplicationContext ctx = new ClassPathXmlApplicationContext("com/helloworld/beans/helloworld-context.xml"); ArtistDao artistDao = (ArtistDao) ctx.getBean("artistDao"); List<Artist> artists = null; try { artists = artistDao.getAll(); } catch (SQLException e) { e.printStackTrace(); } req.setAttribute("artists", artists); try { req.getRequestDispatcher("index.jsp").forward(req, resp); } catch (ServletException e) { e.printStackTrace(); }
script code that suddenly finds a null object
<% List<Artist> artists = (List<Artist>) request.getAttribute("artists"); if (artists == null) { out.println("artists null"); } else { for (Artist artist: artists){ out.println(artist.getName()); } } %>
Even jstl code seems to agree
<c:if test="${artists eq null}"> Artists are null </c:if> <c:forEach var="artist" items="${artists}"> ${artist.name} </c:forEach>
For my application, I use weblogic, spring 2.5.6 and ibatis.
Jeune source share