2009-07-09 5 views
8

Lavoro con diversi server e configurazioni. Qual è il miglior approccio al codice java per ottenere lo schema: // host: [porta se non è la porta 80].Java: rappresentazione di stringa solo dell'host, schema, eventualmente porta dalla richiesta servlet

Ecco un codice che ho utilizzato, ma non so se questo è l'approccio migliore. (questo è pseudo codice)

HttpServletRequest == richiesta

String serverName = request.getServerName().toLowerCase(); 
String scheme = request.getScheme(); 
int port = request.getServerPort(); 

String val = scheme + "://" + serverName + ":" port; 

Tale che val ritorna:

http(s)://server.com/ 

o

http(s)://server.com:7770 

Fondamentalmente, ho bisogno di tutto, ma la query stringa e 'contesto'.

ero anche utilizzare URL:

String absURL = request.getRequestURL(); 
URL url = new URL(absURL); 

url.get???? 
+0

http://stackoverflow.com/questions/883136/is-there-a-good-url-builder-for-java sembra essere una domanda molto simile. – aem

+0

Questa è una domanda piuttosto generica e non posso usare le librerie di apache. –

risposta

16

provare questo:

URL serverURL = new URL(request.getScheme(),  // http 
         request.getServerName(), // host 
         request.getServerPort(), // port 
         "");      // file 

EDIT

nascondiglio porta di default sulla http e https:

int port = request.getServerPort(); 

if (request.getScheme().equals("http") && port == 80) { 
    port = -1; 
} else if (request.getScheme().equals("https") && port == 443) { 
    port = -1; 
} 

URL serverURL = new URL(request.getScheme(), request.getServerName(), port, ""); 
+0

Se la porta è la porta 80, la ripetizione della stringa URL contiene: 80 testo? –

+0

sì, puoi passare -1 per utilizzare la porta dello schema di default – dfa

2
URI u=new URI("http://www.google.com/"); 
String s=u.getScheme()+"://"+u.getHost()+":"+u.getPort(); 

Come detto biscotti, da java.net.URI (docs).

+0

Suoni giusti. Potrei usare quelle funzioni con l'oggetto 'richiesta' del servlet. –

1
public String getServer(HttpServletRequest request) { 
    int port = request.getServerPort(); 
    StringBuilder result = new StringBuilder(); 
    result.append(request.getScheme()) 
     .append("://") 
     .append(request.getServerName()); 

    if (port != 80) { 
    result.append(':') 
      .append(port); 
    } 

    return result; 
} 
0

Se desidera conservare l'URL così com'è apparso nella richiesta (ad es. lasciando fuori dalla porta se non fosse stato dato esplicitamente), puoi usare qualcosa di simile. L'espressione regolare corrisponde agli URL HTTP e HTTPS. Cattura gruppo 1 contiene la radice del server dallo schema alla porta opzionale. (Questo è quello che vuoi.) Il gruppo 2 contiene solo il nome dell'host.

String regex = "(^http[s]?://([\\w\\-_]+(?:\\.[\\w\\-_]+)*)(?:\\:[\\d]+)?).*$"; 
Matcher urlMatcher = Pattern.compile(regex).matcher(request.getRequestURL()); 
String serverRoot = urlMatcher.group(1); 
+0

Ho messo meno solo perché visto qui regexp – msangel

Problemi correlati