What is the difference between relative and absolute url in servlet container. for example if there is an jsp called forum.jsp under webinf folder. when i want dispatch the current request to the jsp from the current jsp file which is under the same webinf folder, is the following correct way
/forum.jsp
relative url means relative to the web-inf folder or to the jsp location.
Absolute URL is : http://stackoverflow.com/questions/3591899/relative-url-and-absolute-url-difference
and a relative URL is: /questions/3591899/relative-url-and-absolute-url-difference
Also, a relative URL can be just: ../questions/3591899/relative-url-and-absolute-url-difference depending where is the linking page located...
Or ./3591899/relative-url-and-absolute-url-difference if the linking page is located on the questions folder
I will suggest to always use Relative URL... and it goes hard, keep trying to use them...
One question, why your JSPs are in the WEB-INF/ folder?
You don't have access to JSP under the WEB-INF folder, if you try to access it the server will throw a 404 error. J2EE only looks for classes and libraries under this folder.
An absolute URL is an URL which includes the scheme (e.g. http:). A relative URL does not include the scheme and is thus dependent on the current context.
How to interpret a relative URL is a bit more complicated. It depends entirely on the context where the URL is been used. E.g. in a webbrowser, or in a servlet, or even in the local disk file system (java.io.File and so on).
When talking in the servlet context, when a relative URL starts with /, it will be relative to the context root (i.e. the root of the webcontent folder, there where the /WEB-INF folder is and where all JSP files are been placed).
So when you want to forward the request to /WEB-INF/forums.jsp, then you just specify that so:
request.getRequestDispatcher("/WEB-INF/forums.jsp").forward(request, response);
But when a relative URL doesn't start with /, then it will be relative to the current request URL. So when the request URL is for example http://example.com/context/servlets/servletname and you use the relative URL forums.jsp, then the following
request.getRequestDispatcher("forums.jsp").forward(request, response);
will actually point to http://example.com/context/servlets/forums.jsp
Related
I am coding my Spring MVC based web application (which was deployed with a war file) and trying to get the value of
String rootDir = request.getSession().getServletContext().getRealPath("/");
I am expecting to get
"C:\user\projects\MyApp"
but the actual value is
"C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyApp\"
I tried to change Source Folder Output Location in Eclipse, but after that all my jsp files, web.xml and folder \WEB-INF are gone. How can I restore them?
The reason I am asking this is that I want my uploaded images to be saved in "MyApp\webapp\WEB-INF\resources\images\" folder.
Why are you expecting the path to be "C:\user\projects\MyApp"?
Your are running your application using Eclipse and Eclipse is deploying it to C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core and not running it from project source folder that is why you get "C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyApp\" from getRealPath() call.
From ServletContext#getRealPath()
Gets the real path corresponding to the given virtual path. For
example, if path is equal to /index.html, this method will return the
absolute file path on the server's filesystem to which a request of
the form http://://index.html would be
mapped, where corresponds to the context path of this
ServletContext.
The real path returned will be in a form appropriate to the computer
and operating system on which the servlet container is running,
including the proper path separators.
Resources inside the /META-INF/resources directories of JAR files
bundled in the application's /WEB-INF/lib directory must be considered
only if the container has unpacked them from their containing JAR
file, in which case the path to the unpacked location must be
returned.
This method returns null if the servlet container is unable to
translate the given virtual path to a real path.
I'm running webapps on Jetty. I have set "dirAllowed" to "false" to disable the directory browsing on the defined contextpath by
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false").
But, users can still access other files (not located on the contextpath) through url (eg. http://somehost.yahoo.com:8081/abc.xml) and abc.xml is located under the root directory of the Jetty server.
Is there a way to block/deny direct url access to files located on Jetty? Thanks!
Not without adding some functionality to your webapp, there isn't. A Java Webapp is essentially a standardized directory structure beginning at the context root (myWebApp in the sample below).
myWebApp/
index.jsp
styles/
mywebapp.css
images/
myimage.png
WEB-INF/
web.xml
lib/
MyLib.jar
classes/
MyPackage/
MyServlet.class
Anything above WEB-INF is directly serve-able, anything below WEB-INF isn't. You could dream up some authorization scheme using Servlet Filters (http://www.oracle.com/technetwork/java/filters-137243.html) and restrict access to content above WEB-INF. Alternatively, if Authentication/Authorization is what you are after, look into Http Authorization and how it can be implemented in Jetty (http://www.eclipse.org/jetty/documentation/current/configuring-security-authentication.html). One way or another, you are going to be some coding or configuration to restrict access to the content above WEB-INF in a Java webapp.
I'm working in maven web application. I need to read a directory(For ex: Files) in my webapp folder as follows,
Java.io.File file = new Java.io.File("path");
But I don't know how to specify the path of the directory here.
You shouldn't give local path addresses. Path should be a relative address, e.g. /files/images under your web archive (.war) folder.
To use relative paths properly, I suggest you to add your target folder to the resources definiton of POM.xml, check out these pages
http://www.mkyong.com/maven/how-to-change-maven-resources-folder-location/
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
You can refer to resources folder easily with something like this:
this.class.getResource("Mydirectory/SubDirectory");
When in doubt how relatives paths work, it's always best to do something like that:
System.out.println(new File("/my/desired/directory").getAbsolutePath());
This will print out the path in which classpath will look for the files.
Assuming:
servlet container webapps dir is located in: /var/lib/tomcat6/webapps
your webapp is called my-webapp.war
You should see the following output: /var/lib/tomcat6/webapps/my-webapp/my/desired/directory
Another pointer: you have mentioned that you are looking for webapp directory. I hope you know that this directory will not end up in *.war - it's contents will.
War files are not always expanded when they are deployed to an app server, so it's possible that a relative path won't exist in a filesystem at all.
Best bets are to use getResource from the class loader, which will return things in the class path (the WEB-INF/lib directory, etc), or to use the getResource() method of ServletContext to find things in the web application itself.
I'm having the exact same problem stated in run applet in web application. It throws a ClassNotFoundException for my applet. I tried the solution from there but still no luck.
Here is my code for embedding Applet in html:
<body>
<applet codebase="/DaaS/applet" archive="/DaaS/applet/firstApplet.jar" code="FirstApplet.class" width="300" height ="300"> </applet>
I've a folder DaaS/applet which contains firstApplet.jar and my index.html is in Daas/Webcontent.
The URL in the codebase (and archive) attribute is relative to the current request URL (the one as you see in browser address bar), not to the disk file system in the server side. Imagine that you've the index.html page in some subfolder like so:
http://localhost:8080/somecontext/index.html
The URL as you have in the codebase (and archive) attribute starts with a leading slash / which makes it relative to the domain root instead of the current folder. So the webbrowser will look for the archive and the JAR in the following URL
http://localhost:8080/DaaS/applet/firstApplet.jar
This may not be correct per se. You need to make sure that the codebase (and archive) URL points to the right URL relative to the current request URL. Based on the information given so far, the /DaaS folder is basically in the same parent as index.html, so this should do:
<applet codebase="DaaS/applet" archive="firstApplet.jar" ... />
(note that I simplified the archive attribute, it will be resolved relative to codebase anyway)
This way the browser will load the JAR from:
http://localhost:8080/somecontext/DaaS/applet/firstApplet.jar
I'm returning a Viewablein my java while which returns a jspwhich is located in the WebContent directory. In that directory there are my cssand jsdirectories where my files are located. My jsp's reference these files using relative paths. e.g js/javascript.js.
However, if I load any of these .jsp's via a return new Viewable("myPage.jsp"); in my .javafiles the paths seem to mess up as when the url is loaded the browser shows the url of the java file and all relative paths fail. How can I correct this issue?
The path you are using should be relative to the resources that return these, rather than to the JSP's themselves. Are these files accessible when you enter their URL's to the browser directly? If yes, then you just need to make sure you put the right path to the JSP files - e.g. use the absolute path instead of the relative one - like this: <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css" />