Suppose a WAR layout like so:
foo.war
-->/WEB-INF
-->/classes (..)
-->/js
-->bar.js
-->index.jsp
-->web.xml
Now suppose the WAR's virtual directory is /blah on server example.com (i.e. http://example.com/blah).
What is the HTTP URL of bar.js, one that would be used in a <script src=""> tag that index.jsp might serve up? http://example.com/blah/js/bar.js doesn't seem to be working.
You must NEVER put a JS inside WEB-INF directory.
As written in Servlet specifications, whatever you put inside WEB-INF directory will never be directly available to the external world. Only local application resources go there.
So if you want some JS file accesible from outside, put it directly at WAR's ROOT. Something like this:
foo.war
-->/js/
-->bar.js
-->/WEB-INF
-->internal resources here
The URL to access JS will be something like:
http://YOUR_IP:8080/foo/js/bar.js
This of course could vary depending on how you setup your war deployment on your application server.
You do however put JSP files inside WEB-INF directory, only to invoke them from Servlets (you can't directly access them either) with something like:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("WEB-INF/index.jsp");
This is a common practice if you don't want people accessing directly your JSP files from outside.
There is no URL that will point to this. Everything within WEB-INF is not exposed to the outside world.
Rather, if you organised the WAR layout like this:
foo.war
-->/WEB-INF
-->/classes (..)
-->web.xml
-->/js
-->bar.js
-->index.jsp
Then you could access your Javascript as http://example.com/blah/js/bar.js.
P.S. You won't be able to access index.jsp either, the way you have things set up at the moment.
Related
I want to create a popup window that shows me all the files in a certain directory like ex a /functions directory. I want to be able to click a file in that directory, click ok, and store it's info in variables (not upload it), How would I do this?
I've tried using a form as:
<form action="" method="post" enctype="multipart/form-data">
But with that I can't specify a directory to open specificlly, I have to manually navigate to that directory.
I've tried using window.open()
window.open("file:///" + "home/user/Desktop/demo/functions");
I've tried using an onclick link mechanism:
<a onclick="file:///+ "home/user/Desktop/demo/functions"">Open folder</a>
None of these seem to work, any ways I could approach this problem?
In JavaScript, file handling gets a bit messy. The only way to grab the contents of a folder from JavaScript would be to connect to a server and have a serverside code in a different language relay the folder information back to JavaScript.
The only way I can think that we could be able to fake this result is by placing an index.html file inside of the target directory. This index.html file would then have the names of all the files in the folder within it. However, they would have to be manually plugged into the HTML file. (if you know how to use PHP, it can scan a directory and push the contents to the HTML file)
When a web browser has to navigate to a folder, it asks the server for an index file (usually this will be an HTML or PHP file). This index would then have the contents of the folder inside of it.
If the folder is indeed on the local computer, however, there is one final way we can do this...
If the page navigates to a folder using a window.location of something akin to file:///C://Users/USERNAME/Desktop/My%20Folder/, chrome (or whatever browser you are using) will navigate to the directory and display the contents of the directory. However, since you can't put JavaScript into this browser-generated index page, you won't be able to manipulate it.
The <input type="file"> is probably your best bet, but you can't set a default directory with it (at least not without some JavaScript voodoo, and even then there are security issues between the web and the local user).
I don't know why you would want to do that, anyway, since directory structures are going to be different between different users, and the specification of paths is different between different OS's.
Instead of doing this I will suggest you copy that picture in project/image folder after clicking the upload button.
I have a simple HTML file with some JavaScript that I would like to run locally (as opposed to deploying to a server). It is embedded inside a larger project whose file structure I would like to maintain. For example, the structure is something like this:
project level folder > src folder containing folders & files I would like to probe
> separate, non-project util folder > HTML & JS files I would like to run against src
I am aware that certain browsers do not allow this for security reasons (as pointed out here), but since I control all of the files - is there a way for the src folder/files to somehow indicate that they will allow the 'separate, non-project util folder' to access them? Maybe some kind of project-specific settings somewhere? I am aware that this can be done in server settings, but as I mentioned above I'd like to be able to run it locally without the need for a server.
The JavaScript that is attempting to access the src files uses RequireJS, in case that helps.
Here is what I ended up doing:
I wasn't able to provide full access exactly this way, but instead I setup a dummy HTML page in the project level folder that clicks itself to redirect to the HTML file located in the separate, non-project util folder. This allowed me to keep everything but that one, very small file separate but not have issues with file access.
There are JSP pages in a folder called 'jsp' inside the WEB-INF. Those pages need some javascript files. In JSP page, javascript files are called
<script type="text/javascript" src="../../app.lib/jQuery-v-1-7-2.js"></script>
but the browser cannot access this resource. All requests to the JSP pages inside the web-inf go through an action and seperate name space also has been given for that pages called 'secure'.
Lets say that, this account.jsp page needs a javascript file which locates in a folder called 'app.lib' inside the 'web' folder.(out of web-inf) .
(1) I want to know a way to retrieve that javascript file into account.jsp page using struts actions or another jsp functionality.
(2) second question is: Lets say, there are images which should be secured, and the application itself should be able to send them to the browser when requested.In other words, images inside a folder in WEB-INF, then how can I access them in account.jsp?
This is the web.xml(session configuration and welcome file list tags removed for clear view)
<?xml version="1.0" encoding="UTF-8"?>
<web-app.......>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
This is the project structure
Don't put your JavaScript files inside the WEB-INF folder. JSP pages kept in WEB-INF are accessed by the servlet-container, processed, and then rendered to the client via the servlet, which has access to files in WEB-INF. JavaScript files are client side resources, and since the client side cannot access any resources inside WEB-INF, they must be located outside that folder.
Typically, it's common to put your JavaScript files in a folder called "js", like so:
-/js
- actions.js
-/WEB-INF
- /jsp
- abc.jsp
As an aside, since JavaScript is client side, even if you were to go through the trouble of storing them in the WEB-INF folder and using the servlet engine to process them, any user would still be able to access the resource URL and view the source. Essentially, you gain no benefit from this and only incur costs, both in maintenance and additional development overhead.
I have a login.fcc page which I want to modularize into Javascript,CSS & HTML modules so that it can be maintained easily.
As you know in Siteminder FCC pages we have number of Attributes which we can access and write code accordingly.
When I try to pull this $$smauthreason$$ in seperate js file I am not able to access the attribute value.(this value shd be set by siteminder when this fcc page is called).
Any ideas why I am not able to put this javascript code in seperate js file?
SiteMinder web agent will only serve .fcc files. If you have another file (.js) you must host it on another server instance, but you will not be able to access the SM attributes from it.
As stated before, the FCC file is the only file processed by the web agent,so you can't do the variable substitution in other files.
I think probably what i'd suggest is binding all the variables to 'global' javascript variables in your fcc file. This would allow you to import javascript source files into the HTML that would have access to the variables, but as javascript variables and not $$USER$$,etc type variables.
The other thing you might play with (and I have not done this) is to try server side includes if you're using apache. If the server side includes are processed BEFORE the fcc handler, then you might be able to modularize more.
Is it possible to get the Javascript server path?
For example, I have a javascript file that is in a directory, and in that directory there are some php scripts that I want to "post" to.
When I include the javascript file in my home page
<script type="text/javascript" src="assets/js/some.js"></script>
I can't access say a file like /post.php that resides in the assets/js/ directory using relative paths since the javascript is not being "run" in the directory that the home page is in.
Obviously, I can specify the path, but I was trying to think of a more robust way.
I know you can get the "script" tags and get the url of the file, and I could probably work from there, but I didn't know if such a function exists, that would just tell you.
Thank you!
Create a script that is served from bla.php and place it before your some.js
<script type="text/javascript" src="bla.php"></script>
Inside bla.php, render a javascript class and store your data in it.
i.e.
function MyConstants() {
this.HELLO = "hello";
this.POST_PATH = "/etc.php";
}
You can access the constants (or anything else) via:
var consts = new MyConstants();
alert(consts.HELLO);
Hope this helps! :-)
You can try to use a cross-browser stack tracer to get the current file name
however there are many cases where your idea this might break, For example if you add a JS cacher that complies all necessary JS files into one and compresses them.
I would say that a smarter approach is to have a principle in place with which you can figure out the urls for your php skripts, like RubyOnRails has routes