jsp:include and incomplete page

J

Jurgen

Hi all,

I have encountered a problem using the jsp:include tag. The
jsp:include calls a servlet which at this stage just returns some
text.

The jsp page...
....
<td>
<jsp:include page="/testServlet" flush="true" />
</td>
....

The output part of the servlet...
....
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.print("Hello!");
out.flush();
out.close();
....

The page shows the text but there is no further html returned after
this point.
Any ideas??

Many thanks

Jurgen
 
A

Andrew Thompson

.....
I have encountered a problem using the jsp:include tag. The
jsp:include calls a servlet which at this stage just returns some
text.

The jsp page...
...
<td>
<jsp:include page="/testServlet" flush="true" />
</td>
...

The output part of the servlet...
...
PrintWriter out = res.getWriter();
res.setContentType("text/html");

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletResponse.
html#getWriter()
indicates you have to call setContentType _before_ getWriter()...
out.print("Hello!");
out.flush();
out.close();

I think the problem here (which would be easier
to sort out with an _U_R_L_) is that your
servlet response is creating a new page and overwriting
whatever was in the JSP - you say..
The page shows the text but there is no further html
returned after this point.

But is there any of the HTML written _up_ _to_
that point the servlet was called? If I am right,
even the opening <td> written by the JSP will
not be in the final output..

HTH
 
A

Andy Flowers

You flush and then close the output stream in the servlet. Let the servlet
container handle this for you.

If you check the server error logs you will probably see some sort of error
that the rest of the jsp html cannot be written to a closed stream.
 
J

Jurgen

Thanks Andy, your suggestion of letting the servlet container handle
the closing of the PrintWriter sorted it all out! Many thanks.

Regarding Andrews post; there was html up to the point of the servlet
being called. I should have made my post clearer, the servlet only
returns a string and the output from it does not include the <html>
and <body> tags. The HTML from the JSP is returned up to the point of
the servlet being called, it just stopped after that point.

I thought the jsp:include tag would only include the servlet's output
only and that closing the PrintWriter object would not affect the
subsequent processing of the calling JSP file after the include
action. I thought explicitly closing the PrintWriter was good
practice.

A more detailed explaination for this newbie would be greatly
appreciated!
 
A

Andy Flowers

For the duration of one http request there is only one PrintWriter object
that can return data, and this is shared by all JSPs and Servlets that are
used for the duration of the request.

Thus you should only close() it if you really have to, such as in the event
of catastrophic errors etc.
 
W

William Brogden

Andy Flowers said:
For the duration of one http request there is only one PrintWriter object
that can return data, and this is shared by all JSPs and Servlets that are
used for the duration of the request.

Thus you should only close() it if you really have to, such as in the event
of catastrophic errors etc.

Explicitly doing an out.flush() on the other hand could be a big help
in determining where a problem lies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top