File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes addHeader() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "addHeader() problem" Watch "addHeader() problem" New topic
Author

addHeader() problem

Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
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

Joined: Jan 24, 2005
Posts: 489

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

Joined: Jan 06, 2006
Posts: 45
Thanks Ram.
Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
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

Joined: Oct 09, 2002
Posts: 294
Try to use setHeader(), although it shouldn't make any difference. What's type of your servlet container?


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
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 ]
 
 
subject: addHeader() problem
 
Similar Threads
xml over http
doPost Servlet called by wget needs addHeader logic
program for displaying request details
request & response
sendRedirect(Url)