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

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");

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 can I access image by calling URL from a webapp

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=.../>

Create downloadable files and store in web app subdirectory in Java

In my web based application (Tomcat7 / JDK 7 - JSP 3.0), I built a translator to take csv files, clean them up, and save the file off. A second part then parses the file for certain information and creates a second file (so it can be loaded into another system for verification). This happens every day. These files are being written to ${ECLIPSE_HOME} (because I'm running this in Eclipse).
What I want to do is have a folder created in the application's root directory {i.e, [app-root]/toBeDLd} specifically for saving the generated files into and downloading them, have my front end JSP be able to read and display the files (like a list of downloadable files, you click the link it downloads in your web browser) and at a specific time, delete all the files in that directory. I don't need to keep them, or have a backup of them.
From what I have read, Sending OutputStream to browser and let browser save it would be a decent starting point, but I have also read some other comments on SO that it is a violation of servlet spec to save files inside of the web app directory. I'm not sure if that is due to it being the root of the webapp or not though. Ideally I would like to keep this completely contained within the web application.
Is this possible? Is this the best way to do this?
Dont put it anywhere under Tomcats webapps. This is probably a very bad idea.
My Suggestion is to add a dataDir init parameter to you web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
<init-param>
<param-name>dataDir</param-name>
<param-value>/var/data</param-value>
</init-param>
</servlet>
You can then use this parameter inside your servlet:
getServletConfig().getInitParameter("dataDir");
If you want it compact with your Tomcat maybe choose $CatalinaBase/dataDir
Whoever deploys the Servlet now can decide where to put the Data.
Maybe an extra partition without execute rights. Maybe an extra fast partition.

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.

Use Spring 3 filters to embed timestamp in resource paths?

Would it work to setup some filters using Spring 3 MVC where the paths for javascript files and css files are modified when streamed to the client, by embedding some timestamp in the filename. And then when those resources are later requested another filter then strips those timestamps out?
This would be an attempt to prevent problems of cached js/css files when an application is redeployed
What would I need to do to set this up? How do I setup the filter to replace the paths with a timestamp and then how to I setup the filter to later strp the timestamps out?
I just need info on the Spring 3 MVC configuration for it in the web.xml, I am ok with what the actual code in the filter will need to do
It may be simpler to use Spring's resource mapping <mvc:resources>, that maps a virtual path to the real location of your CSS and Javascript files. The virtual path can contain the version of your application. This means that when you deploy a new version of your application, the path of the CSS and Javascript that gets sent to the browser is different than before and this fools the browser into thinking that they're new resources - and so it reloads them.
For example to map CSS and Javascript files in /resources:
<mvc:resources location="/resources" mapping="/resources-1.2.0/**"/>
This says that any request that comes in with the URL pattern /resources-1.2.0 followed by anything (e.g. /resources-1.2.0/css/styles.css), look for the file in the folder named resources in the web root.
When you update the application version between deployments the virtual path to the CSS and Javascript resources will change and so browsers will be forced to reload the files - even though the real files are in the same old location.
You can make the application version dynamic too - so you don't need to modify your config file.
There's a more in-depth write up of this whole approach here.

Categories