如何从jsp请求对象获取基本URL? [英] how to get the base url from jsp request object?
本文介绍了如何从jsp请求对象获取基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从jsp请求对象获取基本URL?
http:// localhost:8080 / SOMETHING / index.jsp ,但我想要直到索引的部分。 jsp,在jsp中怎么可能?
How to get the base url from the jsp request object? http://localhost:8080/SOMETHING/index.jsp, but I want the part till index.jsp, how is it possible in jsp?
推荐答案
那么,你想要基本URL 吗?您可以按如下方式在servlet中获取它:
So, you want the base URL? You can get it in a servlet as follows:
String url = request.getRequestURL().toString();
String baseURL = url.substring(0, url.length() - request.getRequestURI().length()) + request.getContextPath() + "/";
// ...
或者在JSP中, < base>
,几乎没有JSTL的帮助:
Or in a JSP, as <base>
, with little help of JSTL:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="req" value="${pageContext.request}" />
<c:set var="url">${req.requestURL}</c:set>
<c:set var="uri" value="${req.requestURI}" />
...
<head>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
</head>
请注意,如果端口号已经是默认端口号,则不包括端口号,例如80。 java.net.URL
不考虑这一点。
Note that this does not include the port number when it's already the default port number, such as 80. The java.net.URL
doesn't take this into account.
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
这篇关于如何从jsp请求对象获取基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文