jsp:include with a servlet returns IOException: Stream Closed

J

jstorta

I have a servlet which returns a list of links to CSS files.

I am trying to include this servlet in the HEAD section of the jsp
page so that the proper style sheets get loaded. I know the servlet
works because I can run it directly from the URL with no problems.

However, when I put this in the jsp file
<jsp:include page="/CSSLoader" flush="true" >
<jsp:param name="style" value="default" />
</jsp:include>


I get this when I load the page
java.io.IOException: Stream closed


Prior to using a servlet, the code was just in another jsp page as a
scriptlet. I included the jsp page the exact same way as I am
including the servlet, and it worked just fine.

It seems to me that when the servlet calls the PrintWriter, the stream
is already closed so it fails. This wasn't the case when the code was
a scriptlet in a jsp file.

The question is, how can I include this servlet in my page before the
stream is closed?

Any guidance would be appreciated. For reference, I have pasted the
relevant method from the servlet below.

Thanks.


protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String style = request.getParameter( "style" );
String browserType = request.getHeader("User-Agent").toLowerCase();
String browserPath = "msie";

if ( browserType.indexOf("msie") != -1 ) {
browserPath = "msie";
}
else if ( browserType.indexOf("gecko") != -1 ) {
browserPath = "gecko";
}

File dir = new File("/cssrepository/" + browserPath + "/" +
style );
String linkStringPrefix = new String("<LINK REL=STYLESHEET HREF='/
myapp/include/css/" + browserPath + "/" + style + "/");
String linkStringSuffix = "' TYPE='text/css'>";

String[] children = dir.list();
if( children == null ) {
System.out.println( "Either the directory does not exist or it is
empty\n" );
}
else {
for( int i=0; i<children.length; i++ ) {
String filename = children;
if( filename.endsWith( "css" ) ) {
out.print( linkStringPrefix );
out.print( filename );
out.println( linkStringSuffix );
}
}
}
} finally {
out.close();
}
}
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top