Show Last Modified Date in JSP

T

teser3

I would like to show the Last Modified Date of a JSP that uses Tomcat
container.
I can do it this way using Java in JSP:
<%@ page import="java.io.*, java.text.*, java.util.*" %> Last
Modified:
<%
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)");
out.println(lastModified);
%>

But I would rather take the Java out of the JSP and use 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.


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;
}
}
 
S

SadRed

I would like to show the Last Modified Date of a JSP that uses Tomcat
container.
I can do it this way using Java in JSP:
<%@ page import="java.io.*, java.text.*, java.util.*" %> Last
Modified:
<%
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)");
out.println(lastModified);
%>

But I would rather take the Java out of the JSP and use 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.

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;
}

}

Since you call Servlet methods and access request object, your
DateBean class should be an HttpServlet class. A stand alone custom
Java class can't access them.If you insist on using a plain Java
class, you should get the JSP file path using more generic means, like
a String literal path string.
 
T

teser3

Thanks, I ended up putting this in a Bean class and hard coding file
name in the Bean class and it works but now I would like to have the
file name passed as a condtion in the JSP Java Bean call.

Here is how it works with hardcoding the file name in the bean class
and calling it in my JSP where it outputs the last modified date and
works great:

Last modified Date:
<jsp:useBean id="datebeann" scope="page" class="data.DateBean" />
<jsp:setProperty name="datebeann" property="*" />
<jsp:getProperty name="datebeann" property="mydate" />


Java Bean class:

package data;
import java.io.*;
import java.text.*;
import java.util.*;

public class DateBean
{
private String mydate = "";

public DateBean()
{
this.mydate = fetchDate();
}

public String fetchDate()
{
String jspPath = "C:\\tomcathome\\mydate2.jsp";
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)
{
this.mydate = mydate;
}
}



The above works but now how can I put the filename in the setProperty
so I can have a variable in the JSP. I tried all the below and it
just prints out a literal value: C:\\tomcathome\\anotherFile.jsp.
Please advise.

Last modified Date:
<jsp:useBean id="datebeann" scope="page" class="data.DateBean" />
<jsp:setProperty name="datebeann" property="mydate" value="C:\
\tomcathome\\anotherFile.jsp" />
<jsp:getProperty name="datebeann" property="mydate" />


Attempt in Java Bean:

private String mydate = "";
private String filename = "";
public DateBean()
{
this.mydate = fetchDate(filename);
}

public String fetchDate(String jspPath)
{

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

File last modified date wrong 8
Date and null 0
Date different 32
last modified script question 1
JSP 1
Adding months to Date 2
servlets and jsp doubt 10
Java Date Formatting 5

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top