• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to display local image in servlet

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys, I hope you can help me. I am dealing with a servlet which needs to print some images. At this moment I am storing them in /var/www/images/ and with apache, I am printing them in my tomcat servlet just with this code:


I think this is not a great solution, so I would like to print them directly with tomcat. I have tried a lot of things I have found in tutorials, but none of them work.
My servlet is located in this path: /var/lib/tomcat7/webapps/servlet/
One of the most repeated advices is that the image must be in that path (or a relative one), so at this moment, the image is stored in servlet/test.jpg.
I am trying to print it with the following code:



but it doesn't work, it just print the typical broken-link-image. I have also tried with src=\"servlet/test.jpg\" but with the same result.
The image owns to the tomcat7 user and group, and it has all the permissions.

Does anybody know hoy to solve this problem?

I also want to use a couple of javascript files, but they doesn't work, and I suppose it's because of the same problem, the source path. I have been dealing with this problem for 3 days now, I think I'm a little stuck...
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another hint: this is what I get in the log file:

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat can't serve files from just anywhere - think what a security risk that would be.

Your image files need to be stored in the web application directory tree and the img link has to be relative to the web application.

Bill
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, William, thanks for your reply. Maybe I didn't explain myself correctly.
The files are stored in the web application directory tree as follows:
/var/lib/tomcat7/webapps/servlet
--/var/lib/tomcat7/webapps/servlet/WEB-INF
--/var/lib/tomcat7/webapps/servlet/test.jpg

So if the relative path is "/", as it is directly stored in the servlet directory, the path in the code should be src="test.jpg" (please, tell me if I am wrong).
Now I am trying just with that, but it doesn't work. Maybe I am forgetting something...
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are wrong. See the JspFaq for information on creating proper server-relative URLs that begin with the context path.
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Bear, thank you for your reply. I am following the Faq you recommended, but I think I am doing more or less the same I was doing. Let me explain: when I run the request.getContextPath() method, the path I got is "/servlet", which I think belongs to "/var/lib/tomcat7/webapps/servlet".
I created a directory there, "/var/lib/tomcat7/webapps/servlet/images", where I put the image "test.jpg". Now, the relative path should be "/servlet/images/test.jpg", or request.getContextPath()+"/images/test.jpg", isn't it?
Anyway, with the following code, it doesn't work either:

I have noticed that in the example code in the Faq, it just says "[...]st.getContextPath() + "ima[...], but as I don't get the "/" after "/servlet", I suppose it's missing.

The strange thing is that now the log returns an OK message, but the image is still missing and instead of it, the browser prints the missing image icon:


Any idea about this?

Thank you.
 
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David ,

Use getServletContext().getRealPath("/") to get the server path and then place your image path


ex : <img src="getServletContext().getRealPath("/")"+/image/test.jpg" />


It will definetely work. Try It
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Subbu, thank you for your reply, but unfortunately, it doesn't work either.
As I am starting to think that maybe the problem is not the path, but it is in other place, I have done the following, just to discard possibilities: I have copied the image test.jpg in the following directories (in all of them):
- /var/lib/tomcat7/webapps/smartgrid/test.jpg
- /var/lib/tomcat7/webapps/smartgrid/images/test.jpg
- /var/lib/tomcat7/webapps/smartgrid/smartgrid/test.jpg
- /var/lib/tomcat7/webapps/smartgrid/smartgrid/images/test.jpg

Now, I try to print the image with this code:



None of these options work, and this is the text I get from the first lines:


CONTEXT PATH: /smartgrid
REAL PATH: /var/lib/tomcat7/webapps/smartgrid/ Image path: /smartgrid/images/test.jpg
Subbu advice: /var/lib/tomcat7/webapps/smartgrid/



As I have said, maybe the problem is in other place and not in the path of the image. Is there a way so I can check if the path works or similar? The log returns the following codes:


1 - /var/lib/tomcat7/webapps/smartgrid/images/test.jpg 404 (I have also tried with "images" instead of "/images", but 404 idem.
2 - /test.jpg 404
3 - /images/test.jpg 404
4 - /var/lib/tomcat7/webapps/smartgrid//images/test.jpg 404
4 - /smartgrid/test.jpg 500
5 - /smartgrid/smartgrid/images/test.jpg 500
6 - /smartgrid/org.apacheblablablabla this is wrong for sure 500
7 - /smartgrid/images/test.jpg 500
8 - /smartgrid/smartgrid/test.jpg 200 (this is OK, but it doesn't show!)



I think there should be more messages, but this is all the info in /var/log/tomcat7/localhost_access_log
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Candamil Santos wrote:Hi, Subbu, thank you for your reply, but unfortunately, it doesn't work either


Of course it didn't work. That's the worst suggestion I've seen in a long time. Of course you cannot use the file path on the local system to reference the image from a remote client. Please ignore that suggestion completely.

Rather than continue to flail, have you been successfully determined what the correct path to the image is simply by typing it into the browser URL field?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Subbu wrote:It will definetely work. Try It


As already pointed out: no, it will not. The file path on the local system is not suitable for use to reference resources in a web page.
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again, Bear. I supposed it won't work, but as I have already tried any idea that came up to my mind and nothing worked, I am trying anything, no matter what.
Regarding the browser URL field matter, is this a valid way to load the image? I mean, when putting that images in the apache folder with the apache server running, they can be loaded, but I never was able to do it with tomcat, I supposed it is no possible. In fact, if I try to load, for example, "http://192.168.2.120:8080/smartgrid/test.jpg", it just loads the main page of my servlet, exactly as if I try to load "http://192.168.2.120:8080/smartgrid". No matter which URL I try to load, it loads always the main page.
I have also tried another thing. The path of my servlet was /smartgrid in server.xml config file. I wanted to keep it as "/" because is the only servlet I am running and I wanted the users to connect to it directly, but I wasn't able to get Tomcat asking fo a password, so I changed it to /smartgrid (and this problem was solved). Anyway, I changed it again to "/" just to check if it solved anything, but once more, nothing happened. Do you think is there any other thing I can try?
Thank you.
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I don't really know if it is important or not, I am printing here the configuration files of my servlet, just in case the problem is there:

server.xml


web.xml in my servlet:


I don't know if it is important or not, but the whole content of my servlet is in the "doPost" function. It works as a state machine, with state 0 when load the main page, and changing it depending on the pressed buttons, so it loads the other parts. The forms use the get method, as with the post it doesn't work when using the "/smartgrid" path.
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn, I changed a couple of things in the server.xml section (crosscontext = false and reloadable = false) and now it works. I am checking what works exactly and tell it in a couple of minutes. Sorry for all the inconveniences.

EDIT: it worked a couple of times and now it doesn't work anymore. Really, I don't understand...
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, problem found! It searches the img in the ROOT directory. Instead of /var/lib/tomcat7/webapps/smartgrid, it searches the image in /var/lib/tomcat7/webapps/ROOT. I suppose is a configuration error...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. Looks like you have things spread across multiple contexts.

And yes, typing the URL of an image in the browser address field is no different than accessing it from inside a page.

The only difference is that inside the page you should be using server-relative URLs that start with the context path, rather than absolute URLs.
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After several tests I am quite sure that the problem is in web.xml file. When I delete that file, or the configuration is wrong, I can't load the servlet, but I am able to load all the images I have stored in the servlet directory. When it is configured as I have written a couple of posts ago, I can load the servlet, but JUST the servlet. I tried to change the "servlet-mapping" section to "/smartgrid", to check if I can load the servlet calling to IP:8080/smartgrid and also the images adding the path of the image at the end, but the servlet doesn't work with that configuration.
Anybody has any idea about how to configure web.xml so it uses the correct context and allows me to load the images?

Thank you.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, mapping / is a big old fail. I don't care if you have one servlet or not. Follow best practices rather than trying to micro-manage the URLs. Unless you are creating something like a REST API, the URLs shouldn't matter. Give the servlet a property mapping.

Secondly, what does the Tomcat manager tell you? Is the context loading correctly?

And thirdly, you said that it's serving images out of ROOT. What are your files doing in ROOT?

 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is that really your web.xml?? No wonder your situation is confused. ALL classes used in a servlet MUST be in a package because without a package the JVM will look in the "current" directory - result very strange behavior.

Bill
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Bear. Yes, you are not the first one who told me that mapping / is a fail, so that's why I am changing it. The more proper, the better.
Regarding the files in ROOT, I just put them there to test if they were loaded from the ROOT directory, and as soon as I checked it, I was able to find the problem. Anyway, it was just a test.
After a couple of tests, I think I found the problem. Because of the way I set the url-pattern, it wasn't working properly. I just changed it to smartgrid/Main, so when calling to /Main it loads the main page, and when calling, for example, to smartgrid/images/test.jpg, it loads the image.
Sumarizing, the problem was in url-pattern.
I just wanted to ask you another question. Let's suppose I have this servlet in this directory, so users need to type the whole address (server.com:8080/smartgrid/Main). I am configuring a port redirection, so they call to 80 and it redirects to 8080. In your opinion, what would be the best option to redirect them to the address of the servlet when they call to the main domain (i.e. server.com), something in ROOT that redirects automatically or other solution?
Thank you.


Hello, William. Yes, this is my web.xml. I followed your advice and create a "main" package to store the Main class, and change the servlet class to main.Main. The problem was not there, but as you think it could be dangerous (and make sense) I prefer to change it as you say. Thank you.
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

Sorry for last reply . That will works for localSystem.

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";

<img src=""+basepath+"var/something/someImage.jpg">

I think this will definetely works . I think you want to show an image that stored in your web application that is in mind i am giving above advise.

What i suggest last time is worst and that will not work if you access your appliation from remote machine. That will used when you want to store any document in your web application.

(If it not helpful ignore it )



 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Subbu wrote:
I think this will definetely works . I think you want to show an image that stored in your web application that is in mind i am giving above advise.


Once again, this is bad advice. Absolute URLs should not be used for resources int he same web app. Server-relative addresses that being with the context path as outlined in the JspFaq should be used.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Candamil Santos wrote:In your opinion, what would be the best option to redirect them to the address of the servlet when they call to the main domain (i.e. server.com), something in ROOT that redirects automatically or other solution?


If you want the web app to be the root web app, make it the root web app.

I dont recommend this -- I always let each web app have a distinct context path and I leave the ROOT web app be -- but if you want the web app to not have a context path (in reality an empty context path), then it should be the root web app. Redirecting from the ROOT is just silly.
 
Manuel Rosales Aguiar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Subbu,
no problem with your advice. I was looking for any help and you just offered me a possible solution ;).

Hi, Bear,
OK, I will talk with my coworkers to see what they want exactly. I think different context is better, but as it doesn't depend on me...

Anyway, I think my doubts are solved now, so I can set the thread as solved. Thank you all for your help.

EDIT: no, I can not, there is no option hahahaha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic