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 is there any getLastModified example? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "is there any getLastModified example?" Watch "is there any getLastModified example?" New topic
Author

is there any getLastModified example?

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi Friends..

Please help me i've not understand about getLastModified..

i've created code below.. but the result is always -1..



can anybody help me how to create good implementation for getLastModified?..
sorry for the really basic question.. i'm confused...

Thanks in advance...


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://osdir.com/ml/user-groups.java.seajug/2005-01/msg00007.html
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Sorry for long reply..

Thanks David for your reply..

After reading it, i'm still confused and not too understand about it..
do you often use getLastModified in your application?..
Would you like to give me a snippet code which implemented in real world?...

Thanks in advance..
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
There is no reason to expect the HttpServlet base class to return a reasonable value.

The usage of the HTTP last modified concept refers to the CONTENT the servlet generates not the servlet instance. The requesting client only wants to know if the content has changed.

This can only be known by the writer of the servlet - ie you the programmer - since you are the only one who knows how the content is generated.

Bill
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

@William
Thanks william for your reply..

assume that the servlet only generates "Hello World" word..
(It's simplified for comprehension)..

and how to check that the "Hello World" word either modified or unmodified since last accesses?..

Thanks in advance..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

The "Hello world" scenario you present is senseless. Since it doesn't represent anything real, how could it have any concept of being modified or not?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Thanks @Bear for your warning..

yap.. i thought it's senseless.. it just used for simplify my comprehension..
would you like to give any code how to implement getLastModified in 'real-world'?...

Sorry for my foolish, i'll learn hard..

Thanks in advance..
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
From HTTP 1.1 header definitions:
14.29 Last-Modified

The Last-Modified entity-header field indicates the date and time at which the origin server believes the variant was last modified.

Last-Modified = "Last-Modified" ":" HTTP-date

An example of its use is

Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT

The exact meaning of this header field depends on the implementation of the origin server and the nature of the original resource. For files, it may be just the file system last-modified time. For entities with dynamically included parts, it may be the most recent of the set of last-modify times for its component parts. For database gateways, it may be the last-update time stamp of the record. For virtual objects, it may be the last time the internal state changed.

An origin server MUST NOT send a Last-Modified date which is later than the server's time of message origination. In such cases, where the resource's last modification would indicate some time in the future, the server MUST replace that date with the message origination date.

An origin server SHOULD obtain the Last-Modified value of the entity as close as possible to the time that it generates the Date value of its response. This allows a recipient to make an accurate assessment of the entity's modification time, especially if the entity changes near the time that the response is generated.

HTTP/1.1 servers SHOULD send Last-Modified whenever feasible.


So - the programmer's task is to set a response header with correctly formatted date.

You could have the Hello servlet code create a String variable with the formatted date/time when it is first initialized and use the addHeader method in HttpServletResponse interface to set the header using that string for every response.

OR
You could keep the long (milliseconds) value from a call to the System.currentTimeMillis() method and use the addDateHeader method which should format the date correctly.

addDateHeader("Last-Modified", timestamp );

Bill
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Thanks @William for your reply..

is it the implementation just like this :..


but, why everytime i try to access that Servlet the request.getDateHeader("Last-Modified") always return -1?..

thanks in advance..

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

From the request, the header of your interest will be if-modified-since...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You really should go ahead and start collecting the specification documents which govern HTTP and thus the servlet protocols.

This short document covers the headers.

HERE is the complete RFC2616 for HTTP 1.1 - keen observers will note the name of the primary authorFielding - the individual most responsible for the current return to RESTful web services.

Googling for his dissertation gets over 4 million hits - find it here.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there any getLastModified example?