tags:

views:

90

answers:

2

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.

+2  A: 

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.

Garis Suero
The last part of your answer is wrong. The `RequestDispatcher` can perfectly access resources in `/WEB-INF`. The intent of placing JSP files there is to hide them from **direct access** (as you say, you will get a 404) so that the enduser is forced to use a servlet to request the JSP file. This has more advantages like ability to use SEO friendly URL's, the frontcontroller pattern, the MVC pattern, etcetera. See also [this answer](http://stackoverflow.com/questions/2523430/hidden-features-of-jsp-servlet/2525995#2525995).
BalusC
Things in WEB-INF and its sub folders are not accessible directly from client, but it works well when you dispatch it (forward or redirect)
Truong Ha
+1  A: 

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

BalusC