Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»

Determining the file type and selecting an icon

We can use the appiication.getMimeType() method to determine the type of file with which we are dealing. This method relies on the file’s extension and the server’s configuration to assign a MIME type. In the getType() method, we determine the MIME type directly, and in the getIcon() method we choose an appropriate icon. If we wanted to get fancy, we could easily group like files together in the listing by sorting on their respective types.

WARNING From scriptlets in the page we can use the application implicit object to gain reference to the current servlet context. However, inside a page’s declared methods (such as our getType() method in this example) the implicit objects are not defined, and we can only use the methods provided by the Servlet and JSP APIs. The current PageContext instance, obtainable through the getPageContext() method, can be used to access any of the implicit objects available to the page.

A more flexible modification date

To make our dates a little more relevant, we decided that any date younger than a week would be shown simply as being modified on the day of the week, rather than the full date. Therefore, something modified on Monday of this week at 4 P.M. says “Mon 4:00pm” instead of “Monday, January 10, 2000 4:00:00 pm.”

Cleaning up file size

We also wanted to convert file size information from a confusing byte format, to terms of kilobytes and megabytes. We simply divide by the appropriate numbers to convert bytes to the appropriate format. This is done in the getSize() method.

Скачать в pdf «Web Development with JavaServer Pages»