relative url and absolute url difference
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 js开发者_C百科p 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
精彩评论