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

addHeader() and setHeader() not working

 
Ben Pheonix
Ranch Hand
Posts: 46
1
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am preparing for OCPJWCD. I am a little stuck on addHeader and setHeader methods. I do understand the difference, but when i am trying to implement it in the code and retrieving the values either via getHeaders() or getHeader(). It is showing value as null. I am adding the code here. Can someone check and let me know, what exactly is going wrong.



What am i exactly doing wrong? Any pointers would be helpful.
Thanks,
Ben
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're confusing request headers (which are sent from the client to the server) and response headers (which are sent from the server back to the client). While both are HTTP headers, there's otherwise no relationship between them.
 
Ben Pheonix
Ranch Hand
Posts: 46
1
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ulf,

Thanks a ton. I got the understanding of what you are trying to tell. I have a question, i am setting headers in the response, in order to see these headers
and retrieve them. What can be done? Can i dispatch the request to another servlet and take out headers from the request? Please clarify.

Thanks,
Ben
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. As I said, response headers are not request headers. You can see them in the client that receives the response, whatever that happens to be (possibly a web browser).
 
Ben Pheonix
Ranch Hand
Posts: 46
1
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ulf,

Thanks a ton. I got the explanation right burnt in the head. Thanks a ton again.

Thanks,
Ben
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A tool like the LiveHTTPHeaders extension for Firefox will show what headers are sent as part of the request, and what comes back as part of the response. That can be very useful for debugging web apps (or for understanding HTTP).
 
Ben Pheonix
Ranch Hand
Posts: 46
1
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ulf,

Thanks so much for a wonderful feedback. I will use this extension and update you with my experience.

Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic