3

我在 localhost 的 8080 端口运行我的应用程序。基本 url 是:http://localhost:8080/EMSApplication/otherparts ...

现在HttpServletRequest我如何提取http://localhost:8080/EMSApplication 部分

目前我正在这样做:

String schema = request.getScheme();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String contextPath = request.getContextPath();
String path = schema + "://" + serverName + ":" + serverPort + contextPath;
System.out.println(path);

这里 request 是HttpServletRequest.

这个程序对吗?

有没有其他方法可以获取信息?

如果我托管我的应用程序并说 URL 是http://my.domain.com/EMSApplication/otherparts ... 那么上述程序代码会起作用吗?


我发现的另一种方法:

String requestUrl = request.getRequestURL().toString();
String contextPath = request.getContextPath();
String path = requestUrl.substring(0, requestUrl.indexOf(contextPath) + contextPath.length());

我需要这个的地方:

HttpServletRequest request = (HttpServletRequest) getRequest().getContainerRequest();
String requestUrl = request.getRequestURL().toString();
String contextPath = request.getContextPath();
String path = requestUrl.substring(0, requestUrl.indexOf(contextPath) + contextPath.length());          
submitLink.add(new WebMarkupContainer("submitImage").add(new AttributeModifier("src", path + "/ASSETS/css/images/login-btn.png")));

我正在开发一个 Wicket 应用程序,我需要在其中指定<img/>.

4

1 回答 1

2

Either way is fine, but I prefer the first. There isn't anything more direct that comes to mind, although if you explained why you want this value, there might be.

于 2012-04-30T18:28:10.903 回答