Forgive me for asking what is probably a simple question; I've been out of JSP development for a few years now. I'm pulling a date field from a database, which returns a java.sql.Timestamp object. I need to compare it to the current date, which I'm presently getting as follows:
Now I need to compare the two to see if now is later than the date I pulled from the database. However, I can't compare the two types (java.util.Date and java.sql.Timestamp.) I need to do this using JSTL. Someone please make me feel silly by telling me how easy it is to do this.
Next dumb question: how did a java.sql.Date make it all the way to the JSP? It should have been converted to a java.util.Date long before. By exposing the java.sql.* instances, you are exposing model layer implementation details that shouldn't be visible to the UI layer. [ May 14, 2008: Message edited by: Bear Bibeault ]
Joined: Jul 19, 2005
Guess it would be easiest just to post some code. I'll redact the irrelevant parts.
Does this help, or are you even more confused now? [ May 14, 2008: Message edited by: Bear Bibeault ]
Oh, you didn't say that you were using the JSTL SQL tags. You do realize that those are not recommended for anything but prototype work, right?
I was originally a bit surprised that the EL comparison failed as Timestamp is derived from Date, but a perusal of the Javadoc reveals:
The Timestamp.equals(Object) method never returns true when passed a value of type java.util.Date because the nanos component of a date is unknown.
which I had forgotten.
Bottom line: you cannot directly compare them.
Solutions? Write a tag or EL function to do either the conversion or comparison in a manner you deem appropriate, or better yet, restructure the app to avoid such pitfalls completely. [ May 14, 2008: Message edited by: Bear Bibeault ]
Here's the blurb from the JSTL Specification itself:
Many web applications need to access relational databases as the source of dynamic data for their presentation layer. While it is generally preferred to have database operations handled within the business logic of a web application designed with an MVC architecture, there are situations where page authors require this capability within their JSP pages (e.g. prototyping/testing, small scale/simple applications, lack of developer resources).
No reputable web developer that I know would use these tags in anything that will actually see the light of day.
Preferred is to follow best-practice tiered web application structure, where the business logic and persistence layers are well-insulated from the UI. You may want to research the term MVC as it applies to web applications. And perhaps this article might be a good start.
At minimum, move any logic out of the JSPs and into Java classes, leaving the JSPs purpose to render the view only. [ May 14, 2008: Message edited by: Bear Bibeault ]
Joined: Jul 19, 2005
Thanks for the information. This is an in-house, not commercial, application, but I'll still try to brush up on the best practices.