• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addHeader() problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ram.
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic