File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addHeader() problem

 
Sara Tracy
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Here <code1> makes a call to <code2>. In code<2>, I first set the mimetype and then a add a header "ekvalue", that I would like to send to the client.

//resp.addHeader("ekvalue", examkey);

Without the addHeader statement, the file contents were being read and sent to the client, earlier. However, after adding "addHeader" statement, just the examkey is being set as part of Response header, and my client can read it. However, the file contents are not being sent. Just one statement is making so much difference !

Is there any problem with the coding?

-Sara


<code1>

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
.........

if (...)
{
..........
String ekey = "ABCDEFG";
PrintWriter pw = response.getWriter();
readData(fName, mtype, pw, response, ekey);
}

.......

}


</code1>

<code2>
private void readData(String filename, String mimetype, PrintWriter out, HttpServletResponse resp, String examkey)
throws ServletException, IOException
{
resp.setContentType(mimetype);
resp.addHeader("ekvalue", examkey);

/// read file contents
........

}
</code2>
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggest you try setting the content type and headers before the call to getWriter().

This is from the javadocs


Sets the content type of the response being sent to the client, if the
response has not been committed yet. The given content type may include a
character encoding specification, for example, text/html;charset=UTF-8. The
response's character encoding is only set from the given content type if
this method is called before getWriter is called.

This method may be called repeatedly to change content type and character
encoding. This method has no effect if called after the response has been
committed. It does not set the response's character encoding if it is
called after getWriter has been called or after the response has been
committed.

Containers must communicate the content type and the character encoding
used for the servlet response's writer to the client if the protocol provides a way for doing so.



cheers,
ram.
 
Sara Tracy
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ram.
 
Sara Tracy
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried setting the content type and headers...but still the same problem



Originally posted by ramprasad madathil:
[QB]Suggest you try setting the content type and headers before the call to getWriter().

This is from the javadocs
 
dema rogatkin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to use setHeader(), although it shouldn't make any difference. What's type of your servlet container?
 
Sara Tracy
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dema. I'll try using setHeader().

I'm using Apache Tomcat.

Originally posted by dema rogatkin:
Try to use setHeader(), although it shouldn't make any difference. What's type of your servlet container?

[ January 22, 2006: Message edited by: Sara James ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic