1 20 21 package com.methodhead.util; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import java.io.File ; 25 26 public class ServletUtils { 27 28 30 32 34 36 41 public static String getRelativeUrl( 42 HttpServletRequest request ) { 43 44 String baseUrl = null; 45 46 if ( ( request.getServerPort() == 80 ) || 47 ( request.getServerPort() == 443 ) ) 48 baseUrl = 49 request.getScheme() + "://" + 50 request.getServerName() + 51 request.getContextPath(); 52 else 53 baseUrl = 54 request.getScheme() + "://" + 55 request.getServerName() + ":" + request.getServerPort() + 56 request.getContextPath(); 57 58 StringBuffer buf = request.getRequestURL(); 59 60 if ( request.getQueryString() != null ) { 61 buf.append( "?" ); 62 buf.append( request.getQueryString() ); 63 } 64 65 return buf.substring( baseUrl.length() ); 66 } 67 68 72 public static String getBaseUrl( HttpServletRequest request ) { 73 if ( ( request.getServerPort() == 80 ) || 74 ( request.getServerPort() == 443 ) ) 75 return request.getScheme() + "://" + 76 request.getServerName() + 77 request.getContextPath(); 78 else 79 return request.getScheme() + "://" + 80 request.getServerName() + ":" + request.getServerPort() + 81 request.getContextPath(); 82 } 83 84 88 public static File getRealFile( 89 HttpServletRequest request, 90 String path ) { 91 92 return 93 new File ( request.getSession().getServletContext().getRealPath( path ) ); 94 } 95 96 98 } 100 | Popular Tags |