Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Include (output of servlet forwarding to JSP)  RSS feed

 
Bruno Collins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet (MnNav), it extracts a data from a database into a bean. The servlet then forwards to a jsp page (MnNav.jsp) that lists the extracted data.
This works fine stand alone. That is, if you just type http://localhost/servlet/MnNav
However, when I make a new jsp page (Include.jsp)and include the servlet (MnNav) like this,
<html>
<%@ page contentType="text/html" %>
<jsp:include page="/servlet/MnNav" flush="true"/>
</html>
and then type http://localhost/Include.jsp , only the html tags are returned. I placed out.println in both the servlet (MnNav) and MnNav.jsp to see if they execute. Executed standalone both run. Executed from the Include.jsp page only the servlet seems to run.
I know you can include the output of a servlet but if the servlet (which produced not output) then forwards to a jsp page (creating output) will this output get included ??
Thanks.
 
Bruno Collins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still having problems with this. If someone could at least tell me whether this is possible or not that would be great.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using the <%@include file %> instead.
the <jsp:include> only includes the OUTPUT of the servlet. if it prosuces no output then why use it?
witht the @ include directive you are actually copy paste the whole servlet to the jsp page.
 
Bruno Collins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the servlet doesn't produce anything directly, but the servlet forwards to a jsp page that DOES produce the output. The servlet is gathering info from a databases that is then used in the jsp page. Am I missing something.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem you might run into by doing it this way, is an IllegalState exception. If your JSP page produces a whole bunch of output, and then 'includes' a servlet that actually 'forwards' to another page for output, then this is a problem.

You can't output (beyond the output buffer size) and then forward to another page, or you get that exception. If you output within the buffer size and then forward, all the output from that first JSP is gone, and you only get the output from forwarding. (which is why you say "only the servlet seems to run")
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the API:

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!