Where to store images, CSS and javascript files on glassfish? - java

I've looked around a lot (trust me :) before posting this questions and I'm still confused. Here's the scenario:
Hosting a J2EE web-app on glassfish v3 using Restlet 2.0 The freemarker template files are located in the /WEB-INF folder. The templates link to jQuery/javascript files, some static images and stylesheets using relative URLs.
Since it's not a good practice to store them in /WEB-INF (as stated at many places on the web) I moved the files to the WebContent folder
WebContent:
+--Images
-.jpg, .gif files
+--Styles
-myStyle.css
+--Scripts
+--jQLabel
+--mColorPicker
+--Images
+--javascripts
-.js files
I refer to them in the freemarker template/html as:
<link rel="stylesheet" type="text/css" href="${baseRef}/Styles/myStyle.css" />
where ${baseRef} is the Root Reference of the site. The site is deployed at /Winbook. So the URL on the localhost for the css looks like this:
http://localhost:8080/Winbook/Styles/winbookwall.css <-- retrieved using a GET
Problem(s):
For each of the above resources I get a 404 :( I'm not sure if this is really how one should store css, images or scripts and whether the WebContent folder is a good place for it be stored.
Question(s):
Why is the above not working? I mean there has to be a mistake and I just don't know about it :)
What is the best place to store the above files on the web server/glassfish?
How do I refer to those resources (in the HTML/freemarker template) if they are deployed in the .war? (They will get deployed in the war file if they are placed in WebContent, right? Is this 'legal' or good practice? Seems to smell :)
Do we have to create alternate doc roots in glassfish for all such resources (or the equivalent Directory class in Restlet?)
What is the 'best' way/place of/for such a deployment for an intranet based application to seamlessly locate the resources? JNDI based lookup? (I don't know how that would work, another question probably on SO ;)
I am totally confused! It's been a while since I had to write a full end to end web app and this was usually taken care of by the 'others' :)
Thanks!

I'm probably late on posting an answer to this but here it is. As I mentioned it Restlet intercepts the URLs and you HAVE to use the Directory class to return static content - initialized with the path "war:///" or "war:///images/" etc., for each of the folders.
The reason it wasn't working was with the Routing issues of Restlet - The directory folder was 'last' in the code order of URLs. Directories are created with Template.MODE_STARTS_WITH and the others (i.e., Restlets or other resources) are Template.MODE_EQUALS
You should either change the order of routing manually or push it at the top of the list of URIs when routing.
Hope that helps anyone stumbling on the same problem.
Here is the related question that Jerome answered: Restlet Routing Nightmare

Try "resource" JARs - see here : http://alexismp.wordpress.com/2010/04/28/web-inflib-jarmeta-infresources/

Related

How to know webjars paths in Spring Boot. How to get paths of all available files in webjars

Many questions on SO refer to invalid paths for various resorces located in Spring-Boot webjars. Currently I have a problem with tether but
I do not want to put another one specifically about tether's webjar files/paths, so want to ask:
Is there any easy way of seeing all contents available through #{/webjars /NAME}?
What is the easiest to debug invalid 404 webjar path in Spring Boot?
How can I see mappings of all that a webjar contains?
Cheers
PS: jquery, bootstrap work fine for me. My tether webjar does not.
<script th:src="#{/webjars/tether/1.4.3/js/tether.min.js}" crossorigin="anonymous"></script>
Silly me.
Look PROPERLY :D into the jar file,
Or even easier - look it up on the http://www.webjars.org webpage.
If you click link under "files" you will be able to see all jar contents, e.g:
https://www.webjars.org/listfiles/d3js/4.10.2

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

What are some conventions to deal with deploying a web app under different root directories?

I am developing a java/javascript web application with JSPs, servlets, JAX-RS, and AngularJS. I am pretty new to this kind of stuff.
One problem I've been having is that I need to address url paths from static html, from javascript files, from jsps, and within servlets. I would like to be able to deploy this web app under different context roots (basically deploy in different folders). But in order to do this, unless I am mistaken, I must go through all the static files, (javascript and html) and change every link to properly reference resources based on new root directory. For the server side files, there are some java convenience methods like ServletContext's getContextPath() which allow me to avoid this.
Are there any conventions or strategies that people use to allow one to easily move a web app to different directories on a site without breaking all the links? At the moment I am guessing you must do search and replace for all the links.
Maybe put a special tag next to all local links so you know to change it?
Cheers,
As Java1 indicates, just use relative paths.
If the resource you are referencing is on the same level as the current web page, you don't need to prepend any path info. For example, if your main web page is loaded from path http://www.example.com/context/hello.html, and you have an image folder with path http://www.example.com/context/image, you can just load a image from that folder in your html with the relative path <img src="image/someimage.png">, without putting hostname or servlet context in your path.
If you are level deeper than the resource you are referencing, use "../" in front of the path. For example: <img src="../image/someimage.png">. If you are two levels deeper, use `../../image/someImage``, etc.
This holds true not just for images, but for scripts or static html as long as the web browser is loading the resource.
The web browser is responsible for converting the relative path to a server path. If you need to load something from the server side, you will need to use the full path.

Protect Java EE webpage

How can I go to the source folder of a project by the browser URL in Java web. I want to ask that my project (consists of JSP and Servlets) is running at http://localhost:8080/myproject/index.jsp in Tomcat. If I want to go to the source folder that is myproject through browser's URL then how can I go? Simply you can tell that I want to hack this project, by knowing this I can prevent this access and can protect my website.
Not sure if this helps you, but if you're using some kind of Controller pattern (e.g. using Struts or a custom servlet that forwards to a JSP), the recommended way to prevent direct access to your JSP pages is to place them in the WEB-INF directory and then forward to these pages from your controller. See here for more details: How to properly put JSPs in the WEB-INF folder?
The Servlet 2.3 standard also defines how to prevent direct access in a similar manner (without putting them under WEB-INF), which then also requires to forward the request to these pages. For details, see here: http://www.jguru.com/faq/view.jsp?EID=471953

Java EE Application Directory Best Practice Question

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.

Categories