Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PrintWriter vs ObjectOutputStream

Status
Not open for further replies.

MarkShark

Programmer
Aug 22, 2001
36
US
I know I can't throw using one and catch using another, so am unsure what the fix is here since I'm not that familiar with writer. I would rather use ObjectOutput/ObjectInput. I've been told writer doesn't work well in servlets.


try {
if (! normalMode) {
dbHelper.setReviewMode (auDataID);
}
String courseFileName = getFileName (String.valueOf (courseID), dbHelper);
File file = new File (courseFileName);
//create absolute path to file so we can resolve relative URLs
//String newFileName = file.getParent() + "\\" + au.getLaunchParams();
//for Unix: use '/' instead of '\'
String newFileName = file.getParent() + "/" + au.getLaunchParams();
BufferedReader buf = new BufferedReader (new FileReader (newFileName));
PrintWriter htmlWriter = new PrintWriter (htmlOut);
String temp;
htmlWriter.write (getAUHtml (au.getLaunchParams()));
htmlWriter.flush();
htmlWriter.close();
}
catch (Exception e) {
e.printStackTrace();
ObjectOutputStream out =new ObjectOutputStream(response.getOutputStream());
out.writeObject(e.getMessage());
out.flush();
out.close();
}
}
 
Check the javadocs:
Code:
getOutputStream

public ServletOutputStream getOutputStream()
                                    throws java.io.IOException


Returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data. Either this method or getWriter() may be called to write the body, not both.

Returns:

a ServletOutputStream for writing binary data

Throws:

java.lang.IllegalStateException - if the getWriter method has been called on this response
java.io.IOException - if an input or output exception occurred

See Also: 

getWriter()

Basically if you are sending binary data such as images, etc use the ServletOutputStream. If you are just sending back plain HTML then a PrintWriter is a much better choice. I don't know who is hating on the PrintWriter but I have never had a problem with it. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top