reset password
Author Message
clee117
Posts: 3
Posted 22:34 Mar 22, 2017 |

While working on Homework 3, I wished to include a view for uploaded images and not just text files. However, I realized that since the uploaded image is under WEB-INF, I cannot use

<img src = "[root_path]/WEB-INF/files.test.jpg">

in my jsp to display the image since- correct me if I am wrong- html cannot display contents behind the WEB-INF folder. If I could, i would move the image files to the outside of WEB-INF, but that would violate Homework 2's requirement of not storing any files outside of WEB-INF/files. In addition, I have also considered passing an array of bytes from the servlet to the jsp file and then writing out the bytes, but doing so would require me to set the content type of the jsp to image/[image_type], but I believe that cannot be possible in a jsp file as that would interfere with the content type of the jsp file itself.

I finally settled on letting a servlet directly handle displaying the image itself since I can freely set the content type and then output the bytes as shown in Download.java from the code examples, but this would not follow the MVC architecture. Thus, I must ask:

1. When downloading, can we download image files from /WEB-INF/files in Homework 3, which was possible when not following the MVC architecture in Homework 2?

2. If so, how can we download image files using the MVC architecture? (Perhaps I was missing something useful from the JSTL library)

EDIT: Before I realized that you cannot use the <img> element in the jsp file, I noticed that this method actually works in Eclipse when running the local Tomcat server, but this does not work in the cs3 server. Does this mean anything?

Last edited by clee117 at 22:42 Mar 22, 2017.
cysun
Posts: 2935
Posted 23:45 Mar 22, 2017 |

I'm not quite sure what you are trying to do, but here are a couple of things that may be helpful.

First, if you have a servlet that can download an uploaded file by id or name (e.g. Download?id=...), then you can embed an uploaded image file in HTML like <img src="Download?id=..." />.

Second, it's a good question that whether a servlet like the Download example should be allowed in MVC because it appears that this servlet, which is supposed to be controller in MVC, is generating a "view" (i.e. displaying an image). I'd say servlets like this is fine in MVC because technically it's sending back a file rather than a "view". In this class we only consider HTML pages as views, and servlets are allowed to generate non-HTML responses when necessary. In general, a web application can employ different view technologies to generate different views. Since JSP is the only view technology we cover in this class, any non-HTML responses can (and in fact, have to be) generated by servlets.

clee117
Posts: 3
Posted 00:01 Mar 23, 2017 |
cysun wrote:

I'm not quite sure what you are trying to do, but here are a couple of things that may be helpful.

First, if you have a servlet that can download an uploaded file by id or name (e.g. Download?id=...), then you can embed an uploaded image file in HTML like <img src="Download?id=..." />.

Second, it's a good question that whether a servlet like the Download example should be allowed in MVC because it appears that this servlet, which is supposed to be controller in MVC, is generating a "view" (i.e. displaying an image). I'd say servlets like this is fine in MVC because technically it's sending back a file rather than a "view". In this class we only consider HTML pages as views, and servlets are allowed to generate non-HTML responses when necessary. In general, a web application can employ different view technologies to generate different views. Since JSP is the only view technology we cover in this class, any non-HTML responses can (and in fact, have to be) generated by servlets.

Thank you for the reply.

In that case, if it is alright with you, I will continue to use a servlet to return only image files when the user clicks on them (which is nearly identical to the Download.java example, but with the exception of the target file being the image file that the user clicked instead). Text files in my web application can already be handled using a jsp file.

Last edited by clee117 at 00:08 Mar 23, 2017.
cysun
Posts: 2935
Posted 00:46 Mar 23, 2017 |
clee117 wrote:
cysun wrote:

I'm not quite sure what you are trying to do, but here are a couple of things that may be helpful.

First, if you have a servlet that can download an uploaded file by id or name (e.g. Download?id=...), then you can embed an uploaded image file in HTML like <img src="Download?id=..." />.

Second, it's a good question that whether a servlet like the Download example should be allowed in MVC because it appears that this servlet, which is supposed to be controller in MVC, is generating a "view" (i.e. displaying an image). I'd say servlets like this is fine in MVC because technically it's sending back a file rather than a "view". In this class we only consider HTML pages as views, and servlets are allowed to generate non-HTML responses when necessary. In general, a web application can employ different view technologies to generate different views. Since JSP is the only view technology we cover in this class, any non-HTML responses can (and in fact, have to be) generated by servlets.

Thank you for the reply.

In that case, if it is alright with you, I will continue to use a servlet to return only image files when the user clicks on them (which is nearly identical to the Download.java example, but with the exception of the target file being the image file that the user clicked instead). Text files in my web application can already be handled using a jsp file.

Yes, that's fine.