Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get header?

 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My servlet:


My view.jsp:


My question: why when servlet sends control to view.jsp, view.jsp doesn't show myheader value?

Thanks!
Jenny
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenny!
According to what I understand, the main point here is that we set headers in the response object and retrieve them from the request object. Therefore, we cannot retrieve the headers set during one request in the same request. But, you will be able to access the header in the next and subsequent requests from the user.
Hope it helps.
[ July 27, 2005: Message edited by: Osama Hasan ]
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Osama, for your quick reply. I think I don't quite get your answer. I tried to put some cookie in the response in my servlet code and then retrieve it from view.jsp and I was able to see my cookie. How to explain that?
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenny!
Sorry for the wrong explanation I previously posted. I played around with your code and came to a simple explanation. There are two types of headers: Request headers, that are sent by the client to the server and Response headers which are sent sent by the server to the client. Both of them are entirely different from each other. When we set a header in the response object we are setting a response header. When we retrieve a header from the request object we are retrieving a request header. This is the reason that the header was not being printed because there was no request header by the name "myheader".
Hope you get my point.
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Osama,

Thanks for your reply. Still confused. Like I said, I put my own cookie in the response in servlet code and then I was able to retrieve it from the request in view.jsp. How to explain that?

Thanks,
Jenny
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you added a cookie to the response object, you also retrieved it from the response object. This is clear, right?
The same way, you added the header to the response object. However, when you wrote this:

You're actually trying to retrieve some header the came with the request. Thus, a header that "exists" in the request. In other words, the "header" EL implicit object maps the header that came with the request (not the ones that you said for the response). Is it clear now?
[ July 27, 2005: Message edited by: Leandro Melo ]
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melo,

javax.servlet.http.HttpServletResponse doesn't have getCCookies() method. getCCookies() is in HttpServletRequest.

Thanks,
Jenny
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! Sorry for that. When I said response for the cookie part, take it as request.
The important thing is if you really understood why you cannot retrieve the header you just added through the use of

The key point is: you "get" header that were sent by the browser (in the request) and not the ones you set/add on the current response.
[ July 27, 2005: Message edited by: Leandro Melo ]
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Melo. I will sleep on it and hope I can get it. I guess I'm confused with diff with cookie and header. why cookie is ok to get but not header? isn't cookie a part of header?
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jingh Yin:
why cookie is ok to get but not header? isn't cookie a part of header?


Ok, I hope I'm not wrong. But just trying to understand better the problem... Can you post the code you used to set the cookie and retrieve it (the code for the servlet and for the jsp)??? Maybe if you send the code, we can go further on this.
[ July 27, 2005: Message edited by: Leandro Melo ]
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenny!
The response headers that we set are intended to be sent to the client to convey additional information about the response.For example try setting the the REFRESH header like this:

response.setIntHeader("Refresh",5);

This will tell the client that the page has to be refreshed after every 5 seconds and you can see the effect. But when you try to retrieve this header like this:

request.getHeader("Refresh");

you will not get the value 5. This is beacuse the client does not send the refresh header to you because there is no need for that. Instead it will send a different set of headers. Just try printing all the request headers sent by the client to you using the request.getHeaderNames() method. I got the following names printed:

accept
accept-language
accept-encoding
user-agent
host
connection

So you can see these headers tell the server something about the client.
On the other hand,we set cookies so that the client can return them to us with every request.
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melo,

My servlet code that adds the cookie in response:

My view.jsp that retrieves the cookie:


Osama,

So what I understand from what you are saying is that, request.getHeader() tells the server something about the client. Because 'myheader' means nothing to the server, so it's being eaten. Is that right?

Thanks guys,
Jenny
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Osama,

In other words, there is no way to retrieve 'myheader' that's being set in the response because server doesn't care?

Thanks,
Jenny
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jingh Yin:

In other words, there is no way to retrieve 'myheader' that's being set in the response because server doesn't care?


Basically it. Because the header you're setting on the response is supposed to go to the browser, then the browser can use it. Later on, when the browser makes a brand new other request to your server, it'll add other header to the request. And this are the ones you can get.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jingh Yin:
Melo,

My servlet code that adds the cookie in response:

My view.jsp that retrieves the cookie:



Ok. Here's what is probably happening.
You're setting this persistent cookie. This means that when you add this cookie, the browser will send it back to you. That's why you're able to get it again. BUT note that when you do cookie.mycookie.value you're actually retrieving the cookie that CAME with the request. NOT the one you just "re-set".

To understand it better, try this.
Close your browser window. Modify the servlet code to set a cookie with a name that you haven't used yet, for example, .
Run your application again. Make the FIRST request to this servlet (it's got be the first one, so you make sure that the browser will not send you this cookie) and see if you get the value of the cookie. You'll see that you won't get the value of the cookie.

Additionally, you can also try to do this.


Make requests to the servlet (just make sure you wait more than 1 second between requests). What we're doing is setting the age of the cookie for one second. This way, after one second the cookie should disapear and the browser won't send it back again. Try to see what happens.
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
So Jenny did you understood what we meant??
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melo & Osama,

Thanks for your patience. I think I get it finally. The request/response pair in my servlet and view.jsp are the same pair. I think I was confused and thought the response object in servlet sends to the request object in view.jsp. so whatever set in the servlet response object will appear in view.jsp request object. Now I know it's not. The response object in servlet is forwarded to the response object in view.jsp. Is that it?

Regarding my confusion on the cookie setting, like Melo pointed out, I was setting a persistent cookie. so the cookie becomes permanent during the whole session.

Thank you so much,
Jenny
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it now Jenny!
There's just one very small point. When you say

Originally posted by Jingh Yin:
The response object in servlet is forwarded to the response object in view.jsp. Is that it?


Is even simpler than this. The response object in the servlet is actually the same in the jsp. In other words, the jsp has a implicit reference to the servlet's response object (remember that a jsp is nothing more than a "special" servlet).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic