• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Header Methods

 
Johnson K Jose
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can some one please tell me what is the real difference between
addDateHeader, addHeader, addIntHeader methods and
setDateHeader, setIntHeader methods. Also their practical use with some code samples?
Thanks in advance
Regards
Johnson
:roll:
 
Jason Qiu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
addDateHeader, addHeader, addIntHeader
will always add a response header with the given name and integer value. This method allows response headers to have multiple values.
you can then use
request.getHeaders(name) to retrieve the muliple values of one header name.
setHeader, setDateHeader, setIntHeader
set a response header with the given name and value. If the header had already been set, the new value overwrites the previous one.
you can use
request.getHeader(name) to retrieve the single values of the header name.
 
Johnson K Jose
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason Qiu,
Thanks a lot for the mail, still I am not able to make the real difference between this addXXX and setXXX Header methods. Can you please give me an Example?
Thanks in advance
Regards
Johnson
 
Jason Qiu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sytax of addXXX and setXXX is excatly the same.
See below:
doGet(req, res){
res.setHeader("content_type", "text/html");
res.setIntHeader("content_length", 1029);
res.setDateHeader("last-modified", 100000000);
}
Note, here, we all use setHeader, becoz this header data may already exists for default. If you use addHeader, it may not effect.
Normally, we use setHeader instead of addHeader in response .
doGet(req, res){
req.addHeader("accept_type", "image/gif, image/jpeg");
req.getRequestDispatcher("newServlet").forward(req, res);
}
Here I use addHeader for the accept_type of request, becoz an accept type may already exists for the request, I don't want to overwrite it, so add it. and Forward it to a new servlet, the new servlet may can return a image/gif or image/jpeg.
 
yogen joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is rather confusing.
If we can use the addXXX method to add multiple header values
Then
What will happen if do the following,
res.addHeader("content_type", "text/html");
res.addHeader("content_type", "text/plain");
???
yogen
 
Jason Qiu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it may depends on the Container.
So don't use like that becoz all will confusing
 
sharana sharana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since addHeader does not check for already existing headers it will add the header values to the specified header if its valid.so adding a mutiple headers in one single addHeader() or one header value at a time will have the same effect.
 
Johnson K Jose
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Thanks for the code Examples
Regards
Johnson
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic