Tomcat doesn't return image resources - java

I'm using eclipse and run my jsp/servlet using tomcat6 from eclipse. My servlet creates for me an image that i store in to a directory in my webapps. But when i try to access to this image from my JSP it returns me that the resource is not available.
What's the problem?
Servlet store image in "myapp/images/saved.png"

You need to move the images directory into the WebContent directory. That will make it work.

Related

Spring boot doesn't serve some of the images from the src/main/resources/public directory

I'm trying to develop a small webapp, for learning purpose, with spring boot,thymeleaf and mysql.I'm using Eclipse IDE
Users can upload images to the app, My Controller class store the images in the src/main/resources/public/images folder and it's URL in the db.
The problem is, Eventhough the images saved by the controller exists in the src/main/resources/public/images directory, They are not directly visible in the public directory from Eclipse, as a result when referred from the served webpages, those images do not show up. Some of the images I manually copy pasted into the public/images folder within the Eclipse .(To do that actually I had to move images to another directory as it's not possible to copy from and paste into the same folder)
Those images are rendered in the served web pages.. The other images saved by the controller, even though they exists in the same directory ,they are not directly visible from Eclipse IDE and they are't rendered in the served pages.
This is Directory's view within the eclipse and all these images are rendered correctly in the served webpages
This is the same directorie's view from the file system, and it contains many images that are not directly visible from eclipse.
And when I include these images in the webpages , they are'nt rendered even though they reside in the exact same folder as other images..
How to get around this issue?
When you run a spring boot or normal spring project using IDE it will bundle everything inside a war and then it serves that bundle so without restarting the server or re-running the application Eclipse doesn't loads the images.
This has nothing to do with spring boot.It is because of the Eclipse IDE as you know if you paste one image inside the folder directory but images wont load without restarting the server or re-running the application.
My recommendation would be to store images on any directory other than your deployed folder directory and access them without any issues.
I have done this inside tomcat folder without any issue.

Making a directory accessible to get files in it through url

I have a directory containing images. Images are added in it at any time, during runtime. I want to make these files visible through some url.
I tried to put this directory in wildfly webapp directory. But the images added at runtime are not visible.
How to make this directory accessible.
You can add a file handler in the undertow subsystem and map it to a URL. In CLI you'd execute something similar to the following to commands.
/subsystem=undertow/configuration=handler/file=images:add(directory-listing=true, follow-symlink=true, path=/path/to/image/directory)
/subsystem=undertow/server=default-server/host=default-host/location=\/images:add(handler=images)
That would list all the images on localhost:8080/images. You can turn off the directory listing and/or use a complete image URL to see the specific image.
In your deployments, create a new directory with the .war extension, as:
$WILDFLY_HOME/standalone/deployments/images.war/
This is an "exploded" deployment, and any new images added there can be reached via:
http://localhost:8080/images/

Open JSP Page directly in brower using URL

I am using Apache-Tomcat-7 and I placed a jsp page( myjsp.jsp ) in
...\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\desktop\
I have started the tomcat and trying the following URL to open the jsp page
http://localhost:8089/desktop/myjsp.jsp
But it is giving 404. I donot want it to open through other means. Can any body tell me that what I am doing wrong?
P.S. localhost:8089 works fine for other applications and tomcate is configured to this port-8089.
EDIT
When I placed the myjsp.jsp in ROOT folder under
...\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ROOT\
and try
http://localhost:8089/myjsp.jsp
Now it works. So what is the reason that in the desktop folder the jsp was NOT found
OR
how can I access myjsp.jsp if it is placed within desktop folder?
Well one way is the way you have already done (by deploying it at the root context of the tomcat server). Although it would seem to work fine, I won't recommend doing that. Changing root context of tomcat to some other application/pages is for other purpose.
The best way to do is to create a small project (you need a dynamic web project with a proper deployment descriptor --> eclipse will do all this job by just a button click), then deploy this project to your tomcat server.
So lets say if you create your project as MyProjectOne, then just place your JSP page (lets call it test.jsp) under WebContent or src/main/webapp folder and you will be able to view your page as http://localhost:8089/MyProjectOne/test.jsp

Where to put jsp project folder in tomcat for hosting?

I am right now working with JSP and using tomcat Apache for that.what my problem is when i am creating one single JSP page and putting in root directory of tomcat then its working fine.but now i have one project that contains some useful jars and other java classes so how to put that whole project directory in tomcat.
I had put that in web app directory but its giving me error as follows
The requested resource (/dailymotion-cloudkey-java-73f6f35/examples/upload.jsp) is not available.
I am Giving snapshot of my web app folder where i had put this folder name as dailymotion-cloudkey-java-73f6f35
i know that i am doing mistake while putting dailymotion-cloudkey-java-73f6f35 directory in tomcat.
but i am totally new in this so i couldn't find out so can anyone tell me
tree Structure of my project
I think dailymotion-cloudkey-java-73f6f35, does not have WEB-INF folder with web.xml file. Due to which it is not able to locate the resources.
or
You can copy your dailymotion-cloudkey-java-73f6f35 project directory into ROOT folder and then try the same URL.
Put it anywhere, but put a context file for your webapp to TOMCAT_HOME/conf/Catalina/localhost which points to for webapps's directory with the docbase attribute. Read details here.

How to get file system path of the context root of any application

I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.
Then I am appending some path as request.getContextPath() + "abc" and create folder.
Then its creating folder in D:// instead of my webapplication folder.
Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.
You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.
Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.

Categories