www.webdeveloper.com
Results 1 to 5 of 5

Thread: Show Last Modified Date in JSP

  1. #1
    Join Date
    Nov 2002
    Posts
    333

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

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    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
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Nov 2002
    Posts
    333
    Thanks for quick response.

    Error when I try and compile the bean class where it has an issue with the request object reference:
    Code:
    Tomcat>javac data\DateBean.java
    data\DateBean.java:16: cannot find symbol
    symbol  : variable request
    location: class data.DateBean
       String jspPath =  getServletContext().getRealPath(request.getServletPath());
                                                         ^
    data\DateBean.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:

    Code:
    <jsp:useBean id="lastModDate" scope="page" class="data/DateBean" /> 
    <jsp:setProperty name="lastModDate" property="*" /> 
    Last Modified: <jsp:getProperty name="lastModDate" property="mydate" />

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    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
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Nov 2002
    Posts
    333
    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles