How can I access image by calling URL from a webapp - java

I have one webapp (abc.war) deployed on tomcat and there is an image folder , under this several images lying and I can access these images by URL: xx.xx/abc/image/hello.jpg
This project is running live but I am not able to get how these images are retrieved.
Now I have another REST based project deployed in tomcat and i want similar behaviour e.g
xx.xx/hellorest/images/anil.jpg, but right now I am not able to access using above URL.
Need help how this can possible. I don't want to use any Servlet to read and write the Image.

First, /WEB-INF is not accessible from the outside.
Second, if you use /images/temp.jpg, the browser will load images from the root of the web server, and not from the root of your web-app (which is deployed under a context path).
Use the JSTL tag to generate absolute paths from the root of the web-app:
" alt=.../>

Related

What's the appropriate directory structure for a spring boot web app so that the home page automatically opens when the program is run?

I've created this project through Spring Boot. It's a full stack web app that takes information from a user, validates it, sanitizes it, authorizes it, and then sends it to a database. It also can retrieve data from a database.
The problem is that, when the project runs, it should open the home page automatically. Instead, I have to navigate to it manually through a browser, resulting in Access Control Allow Origin issues (as it appears that AJAX is being sent cross origin).
My current directory structure is like this:
C:\Users\workspace\Repository\Project\src\main\resources\templates
Within this templates folder, I have my webpages and config folders. These config folders contain the Javascript files.
I've looked at other projects I've created, and directory structure is very different, but they all use jsps. They look like this:
C:\Users\workspace\CapstoneProject\src\main\webapp\jsp
This directory has all the jsps in it, and they start with the project.
What is the appropriate directory structure so that they start with the project? I've googled this and looked on Spring's website but have found nothing.
The location is less important and it depends on your ant/maven/gradle/script of building your artifact for example War file which it should be copied to webapps folder and then folder for resource type as jsp/css/js/html
Static resources as html can be outside your war in a separate resource folder.
According to tomcat it depends on your size of the application:
*.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript,
stylesheet files, and images) for your application. In larger
applications you may choose to divide these files into a subdirectory
hierarchy, but for smaller apps, it is generally much simpler to
maintain only a single directory for these files.

how to store image url in oracle database and retrieve it through ejb

My project is an EJB. I want to store image in the oracle database. I understand its more efficient to store the file path in database and the images in a folder. My folder "images" is located in ejb. Please if the image is java.jpg, how do I construct the file path? I have tried both images/java.jpg and images\java.jpg and none worked.
The real path to the image location is "C:\Users\Kate\Documents\NetBeansProjects\Kate_Assig\Kate_Assig-ejb\images" and "Kate_Assig-ejb" is the ejb project and that's where the images folder that contains the images is.
The challenge I have right now is to store the file path in database. I have tried this option "..\images\java.jpg" and it didn't work. It didn't work because images are not displayed when I query the table from the client side.
The client is web application.
Let say that you stored image in some directory and its path in database and you have just JEB module, though you want to access images over web using URL. What you need to understand is that EJB does not provide web access. You have to create web module as well and configure it to serve your images.
There shall be servlet that accepts image identifier in parameter or in path. It will then load path from database, open the file and return it back. If you store the file in some public place within web module (not under WEB-INF) then you do not need to use database at all. The parameter / path could be constructed that it holds enough information to find and serve it.
There is some tutorial on web module by Oracle: http://docs.oracle.com/javaee/5/tutorial/doc/bnadx.html Basicly you need to create a war file with following structure:
index.jsp
WEB-INF\
classes\
lib\
web.xml
Directory WEB-INF is protected and browser cannot access it. It contains classes directory to store web module classes, lib folder for dependencies (ejb module jar, libraries) and web.xml. This file is important as it defines your servlets, filters and URL mappings.
You can study following samples to implement your solution that will serve images stored on filesystem:
http://www.java2s.com/Code/Java/Servlets/Sendworddocument.htm
http://www.javatpoint.com/example-to-display-image-using-servlet
I hope that this will help you to proceed.

Reloading resources in Java Web Application

I upload an image from hard drive and then store it in some folder in my app.
But the image isn't displayed immediately on my JSP, it is displayed only after I restart the app.
I guess that's because when the app resources are loaded, required image file doesn't exist in the destination folder yet.
So, I think that reloading app resources programmaticaly immediately after storing the image file into the destination folder will solve my problem, but I don't have a clue how to do it.
Update 1:
Part of jsp were the image is asked for:
<img src="${pageContext.servletContext.contextPath}/img/${imageUrl}" />
where imageUrl is the image file name with it's extension.
The images are stored in MyProject/src/main/webapp/img/
Could anyone help me?
Thanks in advance.
I see.. Thanks.
Here is what happens (IMHO :) ) :
During the JSP processing the variable gets resolved to some static path and gets passed to the browser.
Browses renders the static content here, once it sees the path it issues additional http request to the server, but it seems like your server can't find your image statically.
Its due to the fact that you Can't change the war physical structure once it gets deployed.
Think just like you work with a file based representation of WAR and not with exploded directory.
Now what you can do?
Try to create a resource servlet that will get the request to bring the binary image and will return it.
Register this servlet in your web.xml with path like,say, /resource and make it get the get parameter imgName
Example
<img src="resource?imageName=${imageNameYouWant}" />
In your jsp instead of static content in img src tag call the servlet with image as a parameter.
When browser sees this it issues a regular request like before but this time it gets processed by your resource servlet and it will bring the image.
Another thing I would try is just to move the images folder outside the webapp directory.
This directory is tracked by your web container automatically but there is no reason to store the images there as well.
You do like this:
Create a directory like: /myapp/images
You'll still need to create some component on the server side. But this time it can probably be done by creating some configuration on the web server side (no custom servlet).
I'm not sure this will work when you use the first approach...
In JSP point on the server as I've already explained above.
It its an enterprise serious application I would recommend the first approach in conjunction with storing the images in the database but its a different story :)
Hope this helps

Servlet Generated Web Pages Finding Images Inside Of A *.war?

I'm maintaining a legacy Java servlet webapp ( nwp ). My goal is to learn Spring and gradually update the webapp to use Spring as much as possible.
The servlet webapp, nwp, now runs on WebLogic 9.2. It is packaged and deployed as nwp.war. Every HTTP Request gets submitted to a unique servlet, which process the request and prints out a web page/screen. Each servlet will read in various resource files from a remote location outside of nwp.war to use for headers, footers, etc.
Yes, it is primative, which is why I want to update it. It also made sense to have the "include files" in a remote location outside of the war as 3 applications use those files. However, as part of updating the nwp app I plan on consolidating the other two ( similarly primative ) apps into just the nwp. Eventually.
As a first step in converting this application to Spring I have rearranged the directory tree to have these subdirectories under the WEB-INF dir:
images
js
css
The servlet generated HTML references images as
"
My problem is that right now the servlet generated HTML can not find images in the WEB-INF/images directory inside of the nwp.war.
Right now, the nwp.war file contains a file called weblogic.xml to map the URLs for images to where they sit on the server:
<wls:virtual-directory-mapping>
<wls:local-path>/common/resources/images</wls:local-path>
<wls:url-pattern>/images/*</wls:url-pattern>
<wls:url-pattern>*.jpg</wls:url-pattern>
<wls:url-pattern>*.gif</wls:url-pattern>
</wls:virtual-directory-mapping>
I'm new to WebLogic and WebLogic 9.2.
I've tried changing that mapping in a number of was so that the servlet generated HTML will look for the pictures in the WEB-INF/images directory inside of the war.
Is this (servlet generated html finding images ) even possible or am I going to have to use the current system of getting images until I can convert the servlets into JSPs?
Thanks
Steve
The HTML won't be able to directly reference images inside the WEB-INF folder. This is for security. So you have 2 options:
Move the images so that they're directly under / rather than /WEB-INF/
Create another servlet to serve those images
If you decide to use a servlet, you can use ServletContext getResourceAsStream to access images from the /WEB-INF/images directory. For example:
servletContext.getResourceAsStream("/WEB-INF/images/test.jpg");

How do I access the JBoss data directory from inside an application?

I would like to upload files to my the server/default/data directory using a simple upload servlet and be able to access them from within my application, but I can't seem to find how to access it. I tried
getServletContext().getRealPath("data");
but that didn't help.
to get the absolute path, just do:
System.getProperty("jboss.server.data.dir");

Categories