1 16 17 package org.apache.taglibs.io; 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletRequest ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.jsp.PageContext ; 26 27 33 public class URLHelper { 34 35 public static URL createURL(String uri, PageContext pageContext) throws MalformedURLException { 36 ServletContext context = pageContext.getServletContext(); 37 ServletRequest request = pageContext.getRequest(); 38 if ( request instanceof HttpServletRequest ) { 39 return createURL( uri, (HttpServletRequest ) request, context ); 40 } 41 else if ( uri.startsWith( "/" ) ) { 42 int port = request.getServerPort(); 43 String host = request.getServerName(); 44 String protocol = "http"; 45 return new URL ( protocol, host, port, uri ); 46 } 47 else { 48 return new URL (uri); 49 } 50 } 51 52 public static URL createURL(String uri, HttpServletRequest request, ServletContext context) throws MalformedURLException { 53 if ( uri.startsWith( "/" ) ) { 54 int port = request.getServerPort(); 55 String host = request.getServerName(); 56 String protocol = "http"; 57 String path = request.getContextPath() + uri; 58 return new URL ( protocol, host, port, path ); 59 } 60 else { 61 return new URL ( uri ); 62 } 63 } 64 } 65 | Popular Tags |