Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Show Last Modified Date in JSP

Thread view: 
teser3@hotmail.com - 23 Jul 2007 01:05 GMT
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;
   }
}
SadRed - 23 Jul 2007 02:50 GMT
On Jul 23, 9:05 am, "tes...@hotmail.com" <tes...@hotmail.com> wrote:
> I would like to show the Last Modified Date of a JSP that uses Tomcat
> container.
[quoted text clipped - 52 lines]
>
> }

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.
teser3@hotmail.com - 24 Jul 2007 02:30 GMT
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);
 }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.