You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Ulrich Mayring <ul...@denic.de> on 2000/07/14 18:34:43 UTC

code to handle absolute and relative URLs

Hi all,

here's a method that can be used to parse a href attribute and return an
URL string. If something like this were used in all the inclusion
mechanisms, then we'd support relative and absolute URIs everywhere.
This method needs the current HttpServletRequest, if that is not
available everywhere, then it could probably be rewritten to use XSPUtil
methods.

private String getURI(String href, HttpServletRequest request) {
	String scheme = request.getScheme();
	String servername = request.getServerName();
	String port = "";
	Integer i = new Integer(request.getServerPort());
	if (! (i.intValue() == 80)) port = ":" + i.toString();
	if (href.startsWith("/")) {
		return scheme + "://" + servername + port + href;
		}
	else {
		String path = request.getRequestURI();
		path = path.substring(0,path.lastIndexOf("/")+1) + href;
		return scheme + "://" + servername + port + path;
		}
	}

Ulrich

-- 
Ulrich Mayring
DENIC eG, Systementwicklung