我使用的是Glassfish 3.1.2.2。是否可以從java(EJB)代碼確定http端口號(http-listener-1)?我沒有任何連接或請求,因此讀取ServletRequest不是一個選項。從java代碼中確定Glassfish HTTP端口號而不要求
1
A
回答
1
是的,這是可能的。您可以使用Glassfish Admin REST API來檢索服務器的幾乎所有屬性。
對於http-listener-1
使用以下URL的屬性:http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1
一個正常的GET請求會給你一個HTML響應,Accept
頭設置爲application/json
來檢索JSON的響應。
在您的EJB中,您只需使用正確的標題向上面的url發送HTTP請求。這裏有一個例子:
String url = "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Accept", "application/json");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
這會給你類似這樣的結果:
{
"message": "",
"command": "http-listener-1",
"exit_code": "SUCCESS",
"extraProperties": {
"commands": [
{
"path": "create-ssl",
"method": "POST",
"command": "create-ssl"
}
],
"methods": [
{
"name": "GET"
},
{},
{
"messageParameters": {
"address": {
"defaultValue": "0.0.0.0",
"optional": "true",
"type": "string",
"key": "false"
},
"enabled": {
"defaultValue": "true",
"optional": "true",
"type": "boolean",
"key": "false"
},
"jkConfigurationFile": {
"defaultValue": "${com.sun.aas.instanceRoot}/config/glassfish-jk.properties",
"optional": "true",
"type": "string",
"key": "false"
},
"jkEnabled": {
"defaultValue": "false",
"optional": "true",
"type": "boolean",
"key": "false"
},
"name": {
"optional": "false",
"type": "string",
"key": "true"
},
"port": {
"optional": "false",
"type": "int",
"key": "false"
},
"protocol": {
"optional": "false",
"type": "string",
"key": "false"
},
"threadPool": {
"optional": "true",
"type": "string",
"key": "false"
},
"transport": {
"optional": "false",
"type": "string",
"key": "false"
}
},
"name": "POST"
},
{
"messageParameters": {
"target": {
"acceptableValues": "",
"defaultValue": "server",
"optional": "true",
"type": "string"
}
},
"name": "DELETE"
}
],
"entity": {
"address": "0.0.0.0",
"enabled": "true",
"jkConfigurationFile": "${com.sun.aas.instanceRoot}/config/glassfish-jk.properties",
"jkEnabled": "false",
"name": "http-listener-1",
"port": "8080",
"protocol": "http-listener-1",
"threadPool": "http-thread-pool",
"transport": "tcp"
},
"childResources": {
"find-http-protocol": "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1/find-http-protocol",
"property": "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1/property"
}
}
}
正如你所看到的結果中包含(在這種情況下,8080)的端口。
您可以從響應字符串中手動解析值,也可以使用某個JSON庫將響應轉換爲可輕鬆檢索屬性的JSON對象。
如果您的Glassfish Admin界面不受保護,如果您啓用了安全管理,您可能必須發送授權參數與您的HTTP請求,那麼此過程應該可行。
參見:
相關問題
- 1. 如何使用指定的端口號發送Java HTTP請求
- 2. 從節點HTTP請求中刪除端口號至外部API
- 3. 確定機架請求中的HTTP端口
- 4. .NET Framework - 打開UDP端口而不指定端口號?
- 5. 替代http端口?
- 6. 如何從http請求中確定客戶端的唯一性?
- 7. AngularJS $ http POST端口號
- 8. 獲取相應的HTTPS端口號HTTP端口在Tomcat中
- 9. HTTP服務器端口號
- 10. Java代碼有效,但不確定是否滿足要求(對象和類)
- 11. 代碼來確定編號
- 12. 向客戶端發送HTTP響應而不需要總是請求
- 13. nginx代理重定向使用代理的端口號,而不是主機
- 14. 純java應用程序端口到glassfish
- 15. 使用Charles調試HTTP請求到異常端口號的端點需要代理可訪問
- 16. Apache InternalHttpClient:確定本地端口號
- 17. 護協議和端口號從反向代理請求
- 18. 在HTTP「主機」頭參數中是否需要端口號?
- 19. 刪除http URL中的tomcat端口號
- 20. HTTP服務器的端口號
- 21. 錯誤而從Java代碼
- 22. 從Java代碼
- 23. npm http-server替代端口
- 24. java中的HTTP和HTTPS默認端口號常量
- 25. 將Http請求重定向到不同的端口
- 26. JS:正確的語法要求,而不是使用進口
- 27. 角$ HTTP請求到另一個端口
- 28. 端口RSA加密的Java代碼,C#
- 29. git端口 - 網絡要求
- 30. 從本機代碼執行HTTP請求