This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

JSP Include (output of servlet forwarding to JSP)

 
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.
 
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.
 
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.
 
My cellmate was this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!