aspose file tools*
The moose likes Servlets and the fly likes getWriter and getOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getWriter and getOutputStream" Watch "getWriter and getOutputStream" New topic
Author

getWriter and getOutputStream

Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
My servlet parses through an xml file and extracts the "mimetype" for a given "filename". If the mimetype is "text/xml", I use getWriter() to send the contents of the file to the client. If the mimetype is "image/png", I need to use getOutputStream() to send the contents back.

if (mimetype == "text/xml")
{
PrintWriter out = response.getWriter();
// read contents of file (character data) .........
..........
}

if (mimetype == "image/png")
{
ServletOutputStream sos = response.getOutputStram();
// read contents of file (binary data) .......
............
}

Now the problem is that I cannot use both getWriter() and getOutputStream(), together in the same program. How do I go about with this?

thanks.
Sara
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure what the problem is.
You're picking one mime type or the other.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

As a side note, you shouldn't be comparing Strings with the "==" operator.
Use the String.equals method instead.
Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
Thanks Ben !
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Uhmmm...

You're Welcome, I guess.
Did you figure out what the problem was?
Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
I figured out this problem, as it has 2 options as you said earlier - the first if condition or the second.

However, this code is just a small part of the larger program. The difficulty I'm facing now, is that I need to display error messages to the user (see <a> ).


<code>

public class GetFile extends HttpServlet
{
...........
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
..........................
.........................
String fileName = request.getHeader("file");

if (fileName == null)
{
fileName = "script.xml";
out.printn("filename not received as header info, so using script.xml as default");

/* <a> how do I print a message like this one -
out.println("filename not received as header info, so using script.xml as default");
I tried defining a PrintWriter object "out" in this if- condition and tried displaying. It displays this message fine, but the contents of the file script.xml are not displayed later.
*/
}
.................
.................................
............
if ( mimeType.equals("text/xml"))
{
PrintWriter out = response.getWriter();
..............
}

if (mimeType.equals("image/png"))
{
ServletOutputStream sos = response.getOutputStream();
...............
}

.................
}
</code>

I need to display both the file contents as well as error messages. any suggestions?

It is quite confusing - thanks for going through this.
-Sara
[ January 13, 2006: Message edited by: Sara James ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can only send one response per request.

You might want to respond with a page that says:
"No file name found. Click HERE to see data as XML")
With HERE being a link to the xml version of the document.
Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
Thanks for the reply, Ben. Appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getWriter and getOutputStream