You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jacob Kjome <Ja...@syntegra.com> on 2000/12/13 23:29:20 UTC

building URL's?

What is the best way to build URL's without hardcoding them?

Right now, I have tried this:

SERVLET_HOME = request.getScheme() + "://" + request.getServerName() +
":" + request.getServerPort() + request.getRequestURI();

DOCUMENTS_HOME = request.getScheme() + "://" + request.getServerName() +
":" + request.getServerPort() + request.getContextPath() + "/";


This seems to work with a servlet that does posting, but I'm confused by
what is said of getRequestURI() in the servlet API:

*****begin excerpt******
Returns the part of this request's URL from the protocol name up to the
query string in the first line of the HTTP request. For example: 
First line of HTTP request  Returned Value  
POST /some/path.html HTTP/1.1  /some/path.html  
GET http://foo.bar/a.html HTTP/1.0   http://foo.bar/a.html  
HEAD /xyz?a=b HTTP/1.1  /xyz  

To reconstruct an URL with a scheme and host, use
HttpUtils.getRequestURL(javax.servlet.http.HttpServletRequest).

Returns:
a String containing the part of the URL from the protocol name up to the
query string
*****end excerpt*******


It seems to say that it should return the whole path, whereas I am
already specifying that from the scheme all the way up to the server
port.  I need to get the the context + path info (such as /servlet/) and
the actual servlet.

Another problem with what I'm doing is that getContextPath() does not
exist in the servlet 2.1 API.


So, what is the best generic (and most proper) way to get what I'm
trying to achieve?


thanks,

Jake