/    Sign up×
Community /Pin to ProfileBookmark

Show Last Modified Date in JSP

I would like to show the Last Modified Date of my JSP web page. I am using Tomcat Container located on Windows Server.

I am trying to use a Java Bean to show the last modified date.
My below attempt in the Bean class gives me errors with request object and I also need help in getting it to work. Please advise.

[code]
package data;
import java.io.*;
import java.text.*;
import java.util.*;

public class DateBean
{
private String mydate = “”;

public DateBean()
{
}

public String fetchDate(String mydate)
{
String jspPath =
getServletContext().getRealPath(request.getServletPath());
File jspFile = new File(jspPath);
Date lastModified = new Date(jspFile.lastModified());
SimpleDateFormat fmt = new SimpleDateFormat(“EEEE, MMMM dd, yyyy,
h:mm a(zz)”);
return fmt.format(lastModified);
}

public String getMydate()
{
return mydate;
}

public void setMydate(String mydate)
{
fetchDate(mydate);
this.mydate = mydate;
}
}
[/code]

to post a comment
Java

4 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJul 23.2007 — you need to tell us about the errors and how you are using this bean to get date in your jsp page or where ever u are using it
Copy linkTweet thisAlerts:
@floridaauthorJul 23.2007 — Thanks for quick response.

Error when I try and compile the bean class where it has an issue with the request object reference:
<i>
</i>Tomcat&gt;javac dataDateBean.java
dataDateBean.java:16: cannot find symbol
symbol : variable request
location: class data.DateBean
String jspPath = getServletContext().getRealPath(request.getServletPath());
^
dataDateBean.java:16: cannot find symbol
symbol : method getServletContext()
location: class data.DateBean
String jspPath = getServletContext().getRealPath(request.getServletPath());
^
2 errors


If I can get the Bean class working, I plan to call the Last modification date in JSP using JavaBean tags:

<i>
</i>&lt;jsp:useBean id="lastModDate" scope="page" class="data/DateBean" /&gt;
&lt;jsp:setProperty name="lastModDate" property="*" /&gt;
Last Modified: &lt;jsp:getProperty name="lastModDate" property="mydate" /&gt;
Copy linkTweet thisAlerts:
@Khalid_AliJul 23.2007 — Yes that error is obvious. You need to send request object to bean when its initialized. My jsp is rusty(several years passed since I last used it), however here is what you should be able to do. in your been create method

getLastModified(String filePath).

Now instead of trying to use request object in there you are passing the file path for which last modified date is required. It should look like this

public String getLastModified(String filePath){

//message implementation here.

}

then in your jsp page make the following change in your jsp:useBean tag so that it looks like this

<jsp:useBean id="lastModDate" scope="page" class="data/DateBean" >

<%

lastModDate.getLastModified(getServletContext().getRealPath(request.getServletPath()));

%>

</jsp:useBean>

now you can use your logic to get the timestamp. you will need to make sure that property mydate is set in the getLastModified() method with the correct value for you to be able to retrieve it.

Hope this helps..

Cheers
Copy linkTweet thisAlerts:
@floridaauthorJul 25.2007 — Thanks
×

Success!

Help @florida spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...