Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > servlet, how to get request's timestamp and timezone?

Reply
Thread Tools

servlet, how to get request's timestamp and timezone?

 
 
John_Woo
Guest
Posts: n/a
 
      10-23-2006
Hi,

I have a web app which needs to log request's info, including its'
timestamp/timezone.

Can anyone have idea (what can be used from 3rd-party-lib, is only
common one, like apache)?

--
Thanks lots
John
Toronto

 
Reply With Quote
 
 
 
 
Andrea Desole
Guest
Posts: n/a
 
      10-23-2006
John_Woo wrote:
> Hi,
>
> I have a web app which needs to log request's info, including its'
> timestamp/timezone.
>
> Can anyone have idea (what can be used from 3rd-party-lib, is only
> common one, like apache)?


for the timestamp you can use the Date request header. It's not
mandatory, but it should be used often.

For the timezone you can take the ip address of the request and then use
one of those services that map the ip address to the location. If the
timezone is not directly available you can get it from the longitude.

Weird requirements you have
 
Reply With Quote
 
 
 
 
Simon Brooke
Guest
Posts: n/a
 
      10-23-2006
in message <(E-Mail Removed) .com>,
John_Woo ('(E-Mail Removed)') wrote:

> I have a web app which needs to log request's info, including its'
> timestamp/timezone.


RFC 2616 does not require or even suggest that requests should pass either
a timestamp or a timezone, so you can't normally get get this information
from the client. So the best you can do is log the time you receive the
request, which is trivial.

The following RFC 2616 headers should (if present) contain date information

| If-Modified-Since ; Section 14.25
| If-Unmodified-Since ; Section 14.28

To access these, you use

javax.servlet.http.HttpServletRequest
public long getDateHeader(java.lang.String name)

"Returns the value of the specified request header as a long value that
represents a Date object. Use this method with headers that contain dates,
such as If-Modified-Since.
The date is returned as the number of milliseconds since January 1, 1970
GMT. The header name is case insensitive.
If the request did not have a header of the specified name, this method
returns -1. If the header can't be converted to a date, the method throws
an IllegalArgumentException.
Parameters:
name - a String specifying the name of the header
Returns:
a long value representing the date specified in the header expressed as the
number of milliseconds since January 1, 1970 GMT, or -1 if the named
header was not included with the reqest
Throws:
java.lang.IllegalArgumentException - If the header value can't be converted
to a date"
--
http://www.velocityreviews.com/forums/(E-Mail Removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; If any council in the country has anything to say to cyclists
;; about cycle paths, it should be: "We are terribly, terribly sorry."
- Zoe Williams, The Guardian, 13th Sept 2006
 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      10-23-2006
Andrea Desole wrote:
> John_Woo wrote:
>> Hi,
>>
>> I have a web app which needs to log request's info, including its'
>> timestamp/timezone.
>>
>> Can anyone have idea (what can be used from 3rd-party-lib, is only
>> common one, like apache)?

>
> for the timestamp you can use the Date request header. It's not
> mandatory, but it should be used often.
>
> For the timezone you can take the ip address of the request and then use
> one of those services that map the ip address to the location. If the
> timezone is not directly available you can get it from the longitude.
>
> Weird requirements you have


Yeah, weird is right.

I don't think that the HTTP RFC actually defines a Date *request*
header, in fact. I've only ever seen it in the *response*, placed there
by the server.

The request (using IE6):

GET http://google.com:80/ HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET
CLR 1.1.4322; .NET CLR 2.0.50727)
Host: google.com
Proxy-Connection: Keep-Alive


The response:

HTTP/1.0 302 Found
Date: Mon, 23 Oct 2006 13:18:43 GMT
Content-Type: text/html
Cache-Control: private
Server: GWS/2.1
Location: http://www.google.com/

Note, no Date header in the request.

So, to answer the OP's question, use your own Date (new Date()) to get
the time of the request (to within a VERY close approximation of the
actual time it was received - I'm talking a few milliseconds, anyway).

And if you *REALLY* need to figure out the time zone of the caller,
probably IP geolocation is your only way of getting a reasonable
approximation. But that is a very strange thing to want to do,
especially for EVERY request. That is the sort of thing that you can get
through post-processing your logs, which will significantly improve your
user's experience with your service.

Rogan
 
Reply With Quote
 
Andrea Desole
Guest
Posts: n/a
 
      10-23-2006
Rogan Dawes wrote:
>
> Yeah, weird is right.
>
> I don't think that the HTTP RFC actually defines a Date *request*
> header, in fact. I've only ever seen it in the *response*, placed there
> by the server.
>
> The request (using IE6):
>
> GET http://google.com:80/ HTTP/1.0
> Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
> application/x-shockwave-flash, application/vnd.ms-excel,
> application/vnd.ms-powerpoint, application/msword, */*
> Accept-Language: en-us
> User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET
> CLR 1.1.4322; .NET CLR 2.0.50727)
> Host: google.com
> Proxy-Connection: Keep-Alive


I see. I looked around a bit, and I found this:

http://www.w3.org/Protocols/rfc2616/....html#sec14.18

<quote>
Clients SHOULD only send a Date header field in messages that include an
entity-body, as in the case of the PUT and POST requests, and even then
it is optional. A client without a clock MUST NOT send a Date header
field in a request.
</quote>

So it makes sense that in this example there is no Date header, because
it's a get.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
xquery - function to get max timestamp nlounello@gmail.com XML 0 12-27-2007 06:44 PM
Get timestamp or current time of client computer. =?Utf-8?B?UmFrZXNoIFZpamF5KHJha2VzaHZpamF5QGhvdG1haWwuY29tKQ==?= ASP .Net 0 11-16-2006 06:14 AM
Can I get timestamp information from Firefox history? JM Firefox 3 10-21-2006 06:12 PM
how do i get a SQL timestamp into/out of a grid? Craig G ASP .Net 5 03-01-2005 09:35 PM



Advertisments