String myDate = new DateServlet().fetchDate(request);
Why on earth would you do something like this? Servlets are not intended to by instantiated by anything but the container. Why aren't you using a bean? (Or better yet, why not follow best practices and not do any Java in a JSP in the first place?)
I originally tried this with a Bean class but couldnt use the request reference object because it doesnt seem to be available. Here is my attempt for the Bean:
Please advise if this is the right direction and what line should I change because the request object is not available when I try and compile it:
Thanks
[ July 21, 2007: Message edited by: Dan Parsons ] [ July 21, 2007: Message edited by: Dan Parsons ]
Originally posted by Dan Parsons: I originally tried this with a Bean class but couldnt use the request reference object because it doesnt seem to be available.
You're passing the request as a parameter. That makes it available no matter what.
Please advise if this is the right direction
Before doing that we need to figure out what you're trying to do in the first place. What is it? It looks like you are trying to get the date of the JSP file. Why? And your approach will not work if the JSP is traversed to as the result of a forward or include.
Thanks, your info has helped alot because in future I will not display Java code in JSP AND I now will concentrate on using beans for displaying objects in JSP.
Before doing that we need to figure out what you're trying to do in the first place. What is it? It looks like you are trying to get the date of the JSP file. Why? And your approach will not work if the JSP is traversed to as the result of a forward or include.
Trying to output the last modified date in JSP page called showDate.jsp:
You're passing the request as a parameter. That makes it available no matter what
How can I use the request object reference in the bean class file (because it wont compile) or should I use something else in the fetchDate method? [ July 21, 2007: Message edited by: Dan Parsons ]
What are you trying to get at in your method? It's considered poor form to pass container objects like the request around. Better to get the data you need and pass that.
I thought I could get the last modified date and return it as a String in the fetchDate method.
This works if I just put it in a JSP with Scriplet:
I would rather not do it this way and use a Bean as you suggested and for giving me guidance to do it correctly in the future. [ July 21, 2007: Message edited by: Dan Parsons ]
Sorry to keep this going but 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 just as I wanted:
Java Bean class:
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.
Originally posted by Dan Parsons: I want Last Modified to show in my JSP to give a Date and time of when the info was last modified/changed in the Web page.
That part I figured. What I should have asked is, why?
To re-iterate and expand on what Bear said earlier, in most up to date Java web applications, the JSPs are only one part of the mechanism that handles the request/response chain. If a JSP is forwared to or included in another page, you're not going to be able to dynamically read this information. Also if your application has been deployed as a packed war file, then the method getRealPath, that you were using earlier to locate resources withing the app will return null; breaking your logic.
It sounds like you're going through a lot of trouble to build something that's only going to work in very specific cases.
Maybe if you let us know what you're trying to accomplish (the bigger picture) someone can suggest a better alternative.
Maybe if you let us know what you're trying to accomplish (the bigger picture) someone can suggest a better alternative.
I have three static JSP web pages where each has information on it and each time I update a page I would like the last modified date to show up on the page to show when that specific page was updated. This web app has just 3 static JSP web pages in it and does not use any MVC or other frameworks. [ July 24, 2007: Message edited by: Dan Parsons ]
Post by:autobot
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, the permaculture playing cards