This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

getWriter and getOutputStream

 
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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 VI Editor Redhat
  • 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 VI Editor Redhat
  • 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 VI Editor Redhat
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!