2014-02-14 48 views
8

如何獲得運行我的Vaadin webapp的應用服務器的協議,IP和端口?我使用Vaadin 7如何獲取Vaadin 7應用服務器的IP和端口?

在vaadin 6我這樣做,但不是在vaadin 7工作:

String server = ((WebApplicationContext) this.getContext()).getHttpSession().getServletContext().getServerInfo(); 

java.net.URL url = this.getURL(); 
String s = url.getHost()+":"+url.getPort()+url.getPath(); 

回答

10
System.out.println(UI.getCurrent().getPage().getLocation().getHost()); 
System.out.println(UI.getCurrent().getPage().getLocation().getPath()); 
System.out.println(UI.getCurrent().getPage().getLocation().getPort()); 
6

我不喜歡這樣寫道:

String basePath = Page.getCurrent().getLocation().getScheme() + ":" + 
        Page.getCurrent().getLocation().getSchemeSpecificPart() 
1

至於應用程序路徑部分,要獲取真實應用程序目錄而不是瀏覽器請求路徑,請使用VaadinServlet.getCurrent().getServletContext().getContextPath()

相關問題