Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getWriter and getOutputStream

 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the problem is.
You're picking one mime type or the other.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, you shouldn't be comparing Strings with the "==" operator.
Use the String.equals method instead.
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben !
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhmmm...

You're Welcome, I guess.
Did you figure out what the problem was?
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Ben. Appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic