Discussion Locked

Java class stdout MIA if included in JSP

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am using JSP. I have a package of java classes that write error
messages to standard out when they catch an error. Works fine on the
command line. Problem occurs when using in JSP. I include them with a
page directive (importing the package)rather than use them as beans,
since they do so much more than beans do.

<%@ page import="OPS.*" %>

When errors occur, messages to "standard out" don't appear in the html
output of the JSP. I don't know where they are going (they aren't in the
web server's error log), but they aren't going where I expected them to.
I figured that standard out was the html page (httpServletResponse). Where am I going wrong
here?

java class uses
catch (Exception e)
{System.out.println("Exception caught: ");
System.out.println(e.getMessage());
return false;
}

I never see the above error message when including this as JSP, but see
it when using a wrapper class invoked at the command line. The method
call that would
generate this message is inside the body of the html, if that matters at
all. Output generated after the error would have occured shows up, so
it's not a buffer issue AFAICT.

Any ideas?
Thanks!

4 replies

by jay eckles

You made an incorrect assumption: stdout is not the web page; it's the console of the web server (assuming the web server has a console). For instance, if you launch an instance of Weblogic 5 on NT, it opens up a console window...any messages printed to System.out appear there.

If you want to write to the browser, you need to use something like
PrintWriter out = response.getWriter() ;
or
OutputStream out = response.getOutputStream() ;
Where response is the implicit HttpServletResponse object in a JSP. You could modify your classes to accept the output stream to print to as a parameter to a constructor, and then modify the methods to use that output stream. Something along these lines (not tested):

public class YourClass extends SomeOtherClass{
private OutputStream out ;
YourClass( OutputStream o ){
out = o ;
}
YourClass(){
out = System.out ;
}
public void writeError( Exception e ){
o.println( e.getMessage() ) ;
}
}

You could then use the class by instantiating it:
YourClass yc = new YourClass( response.getOutputStream() ) ;
and using it

yada yada yada ...
<%yc.writeError( exception );%>

Good luck.

Jay Eckles

by jay eckles

By the way, perhaps it helps "connect the dots" if I mention that in JSP
<%=foo%>
is equivalent to
<%
PrintWriter out = response.getWriter() ;
out.print( foo ) ;
%>

by nikki96

I am rating this acceptable, though it didn't work, because I found out Netscape Enterprise Server 4 (what I'm using) has a bug that prohibits JSP from using response.getOutputStream() properly. Since this code worked fine (with an addition of PrintWriter out, not OutputStream) from the command line and from servlets, I'm assuming it's just my server that bites and this would work on a real server like Apache/Tomcat.
Thanks for the effort!

This discussion is closed to new replies.