I have an JSP page where I want to display an image from a specific folder of my computer. Can any body help me how can I do this ? Another issue is, I want to read a text file from a specific folder by my JSP code, can anybody help me how can I achive this ?
Asked
Active
Viewed 1,332 times
0
-
2See http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page. The technique is identical, except you must read the image bytes from the file system rather than reading them from the database. – JB Nizet Dec 30 '11 at 22:29
1 Answers
0
You can use an HTML img tag to display the image. As long as the image is in the same directory as your jsp file, you can just reference it directly.
As far as reading the file goes, it really depends on what exactly you're trying to do here. For a complex web application, (in the interests of keeping your model and view separate) you need to use a lot of different technologies like JSTL and java beans, and maybe even a framework like Spring. If what you're trying to do is simple you can "cheat" and read the file with a scriptlet, just don't expect the code to scale to anything complicated.

Ring
- 2,249
- 5
- 27
- 40
-
2-1: scriptlets in JSP, file not closed in a finally block, catch (Exception), use of a DataInputStream where it's not needed. Plenty of bad practices. – JB Nizet Dec 30 '11 at 22:45
-
You're still suggesting to use scriptlets, and you don't really answer the question. He asked how to read an image from a specific folder. You don't tell him how to read a text file, and although Spring can be helpful, a simple servlet is sufficient to decouple Java code from the JSP. – JB Nizet Dec 30 '11 at 23:12