Java EE Application Directory Best Practice Question - java

I am relatively new at Java EE and have just gotten familiar with the traditional application directory structure (which includes the BUILD, DIST, NBPROJECT, SRC, and WEB folders). I was wondering: if my application requires to use some resources/assets (such as images or pdfs) on the server, in which of the 5 folders listed above should I place those resources? I think I can technically place them anywhere, as long as I reference the files correctly, but not sure what the common best practice is for this.

You would usually include static content e.g. images or PDFs which need to be read by http requests in WebContent e.g. if my-web is your WAR project:
my-web/WebContent/images
my-web/WebContent/scripts
my-web/WebContent/misc
This means you can access using for example:
/my-web/images/bob.jpg
/my-web/scripts/bob.js
/my-web/misc/bob.pdf
Storing the files here allows public access. I will quote this from the Eclipse help pages:
The Web content folder represents the
contents of the WAR file that will be
deployed to the server. Any files not
under the Web content folder are
considered development-time resources
(for example, .java files, .sql files,
and .mif files), and are not deployed
when the project is unit tested or
published.

You need to think about how the assets get delivered to the client before deciding where to put them. Resources such as ccs and static html pages are retrieved by the client using separate HTTP calls, so they need to be in a place that is accessible. In your case, I'd say they have to be at the level of index.jsp, or in subdirectories at that level. If you put them in WEB-INF, they will be inacessible from the outside world. You could possibly put them in META-INF but it would be an unusual use of that directory.

Related

404 on HTML content (Servlets) [duplicate]

I have a web application that contains hundreds of HTML, JavaScript and image files. These files are located under the root directory:
my_root--
-- html
-- js
-- images
These folders contain some subfolders.
From a security reason I need to move all these resources under the WEB-INF folder so they will not be directly accessible.
Currently JSP and servlet files are already under the WEB-INF folder.
What is the easiest method for me to safely move all HTML/JavaScript/images folders under the WEB-INF without breaking all links/forwarding to resources in these folders and make sure these resources are not directly accessible?
I am using WebSphere and WebLogic servers.
What is the easiest method for me to safely move all html/js/images folders under the WEB-INF without breaking all links/forwarding to resources in these folders and make sure these resources are not directly accessible?
You're making a thiniking mistake here. HTML/JS/image (and CSS) resources need to be directly accessible anyway. For JSPs the story is different, some of them, if not all, need to be preprocessed by a servlet (e.g. to retrieve some list from DB for display in a table). If those JSPs were been accessed directly, then that servlet step would be skipped altogether, which is absolutely not what you want (the JSPs end up "empty"; without any data from the DB). That's why they should be hidden in /WEB-INF to prevent direct access without going through a preprocessing servlet first. Also, in case of servlet based MVC frameworks, this way the whole MVC framework process (collecting request parameters, converting/validating them, updating model values, invoking actions, etc) would be skipped.
Your concrete functional requirement is not exactly clear (the whole question makes at its own no sense; the answer is just "don't do that"), but if you actually want to restrict access to static resources which don't need to be preprocessed by a servlet at all to certain users only, then you need to implement an authentication/login system. You can utilize container managed authentication or homegrow a Filter for this.
You can go with a very simple tool like notepad++ and use the findAndReplace feature. Eclipse can also do this but it gets tricky to effectively find every reference.
Note that there are other ways to stop users from accessing your images. It is probably easier to just leave things where they are and instruct the websphere to stop serving these images from the images folder

java.nio.file.DirectoryStream in WAR

I'm trying to work with nio's way to read files from directory.
I've come across the java.nio.file.DirectoryStream
which is cool because now I don't need to maintain a list of files in the directory by myself and I like this idea.
Now, my code is going to be run inside the WAR so that it will read the resources (say some xml files) packed inside jars inside this WAR.
I couldn't find a good example of working with DirectoryStream like this. So my question is how can I use this approach (not necessarily this particular class of course) in my situation?
Maybe there are some thirdparties that provide the similar abstraction...
In addition I would like to test this code from my unit test (which means that ideally this code shouldn't really assume that it runs inside/outside the WAR if its possible of course).
Thanks and have a nice day
I would suggest creating a temporary folder and copying the needed resources into this directory. Now you can use the DirectoryStream-Class, because the files are placed in the filesystem.

Set path for js files generated by GWT

I have GWT application and I need to use files, generated for client side in Java EE application that doesn't use GWT. Actually, I just want to include my_app.nocache.js in some html page.
This part works fine, problems begin when my_app.nocache.js tries to load other resources, generated by GWT (other *.js files and *.css files). But I have all my resources in /_resources/mypackage/ and my_app.nocache.js doesn't know anything about this and tries to load just my_name.css instead of /_resources/mypackage/my_name.css
Is there any way to set up path for GWT to generate resource files, so it wold be same, as I use in my Java application?

Getting resources from jars in an Applet's cache

I'm shoehorning an existing application into an Applet so we can say we support Web 2.0. Don't ask.
File system accesses are sprinkled all over our baseline so I'm in the process of converting them all to classpath based resource loading which is going swimmingly, except for the dozens of cases where we're traversing the directory system compiling lists of things. One simple example is looking inside a particular directory for .obj files and presenting that list to the user.
So I wrote an abstraction layer over files and directories in jars vs the file system, and it works just fine. My problem is that when I get the resource URL from class.getResource it contains the address for the jar on the server, but I know that the Applet has already accessed that jar for various classes.
I'm hoping there's a way to avoid getting the jar file from the server; e.g. the file is already cached somewhere and there's an easy way of obtaining it.
What you are seeing is deceptive. It is no business of applet and JWS apps. to know the location of cached resources. For that reason even if the resources is cached locally, the plug-in will claim the path is to the server.

Using the ClassLoader method to retrieve all resources under classes as Input Streams

My problem is one that you would think is quite common, but I haven't so far managed to find a solution.
Building a Java web app under Tomcat 5.5 (although a requirement is that it can be deployed anywhere, like under a WebLogic environment, hence the loading resources as streams requirement). Good practice dictates that resource files are placed under WEB-INF/classes and loaded using the ClassLoader's getResourceAsStream() method. All well and good when you know the name of the resource you want to load.
My problem is that I need to load everything (including recursively in non-empty sub-directories) that lives in a subdirectory of classes.
So, for example, if I have the following under WEB-INF/classes:
folderX/folderY
folderX/folderY/fileA.properties
folderX/fileB.properties
I need the fileA.properties and fileB.properties classes to be loaded, without actually knowing their names before the application is started (ie I need the ability to arbitrarily load resources from any directory under WEB-INF/classes).
What is the most elegant way to do this? What object could I interrogate to find the information I need (the resource paths to each of the required resources)? A non-servlet specific solution would be best (keeping it all within the class loading framework if possible).
Thanks in advance!
As far as I am aware, there is no such ability, since the classloader only attempts to load things it is asked for. It doesn't pre-fetch all items on the classpath, or treat them as a directory structure.
The way I would solve the problem is create a directory listing in a text file of all relevant resources at build time and include that in the war, and then walk it through that way.
You can do that with some tricks :)
Get the resource as URL, extract the protocol :
file protocol - get the URL path and you have a folder, scan for files.
jar/zip protocol - extract the jar/zip path and use JarFile to browse the files and extract everything under your path/package.

Categories