i have a jsp. which calls a servlet on jsp load and display the results in same jsp as below.
Some.jsp
<html>
<jsp:include page="/HelloWorld"/>
<%Iterator itr;%>
<% List data= (List)request.getAttribute("results");
for (itr=data.iterator(); itr.hasNext(); )
{
%>
<TABLE align="center" cellpadding="15" border="1" style="background-color: #ffffcc;">
<TR>
<TD align="center"><%=itr.next()%></TD>
</TR>
</TABLE>
<%}%>
</body>
</html>
in servlet i am storing results in request and using requestdispatcher to invoke the jsp as below.
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
List<String> list = new ArrayList<String>();
//some logic to populate list
request.setAttribute("results", list);
request.getRequestDispatcher("/WEB-INF/Some.jsp").forward(request, response);
}
}
But i am getting below exception while displaying results in jsp:
java.io.IOException: Stream closed
at org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:202)
at org.apache.jasper.runtime.JspWriterImpl.clearBuffer(JspWriterImpl.java:157)
Please help me..
The JSP includes the servlet, which forwards to the JSP, which includes the servlet, which forwards to the JSP, which includes the servlet, which forwards to the JSP, which includes the servlet, which forwards to the JSP, which includes the servlet, which forwards to the JSP, which includes the servlet, which forwards to the JSP, which includes the servlet, which forwards to the JSP…
You have a serious design issue here. Adopt the MVC principles: all requests go to a servlet (Controller), which loads the Model, and dispatches to the appropriate JSP (View). A view should not include a servlet, and certainly not in a recursive way like this.