js中获取 <%=request.getContextPath()%>
2013-09-04 16:54
489 查看
在jsp中获取context path或者basePath 是很容易的。在javascript中也可以获取,并且有的时候也很有必要。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
在单独的javascript中不能使用<%= basePath%>类似这样的java script所以,只能用javascript来获取此context path.
可以用下面的代码来实现。
var localObj = window.location;
var contextPath = localObj.pathname.split("/")[1];
var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath;
var server_context=basePath;
两种实现的方法用在不同的环境中。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
在单独的javascript中不能使用<%= basePath%>类似这样的java script所以,只能用javascript来获取此context path.
可以用下面的代码来实现。
var localObj = window.location;
var contextPath = localObj.pathname.split("/")[1];
var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath;
var server_context=basePath;
两种实现的方法用在不同的环境中。
相关文章推荐
- 转载【js中获取 <%=request.getContextPath()%>】
- [js]js中获取 <%=request.getContextPath()%>
- js中获取 <%=request.getContextPath()%>
- js中获取 <%=request.getContextPath()%>
- <a href="<%=request.getContextPath()%>/XXXX.jsp"> 和<a href="XXXX.jsp"> 的区别?
- <%=request.getContextPath()%>
- 网页代码中用<%=request.getContextPath()%>和不用的区别?
- ${pageContext.request.contextPath}和<%=request.getContextPath()%>
- <%=request.getContextPath()%>的作用
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- <%=request.getContextPath()%>的作用
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- 路径问题 解决 ${pageContext.request.contextPath} <%=request.getContextPath()%>
- <c:set var="root" value="${pageContext.request.contextPath}"></c:set>According to TLD or attribute d
- <%=request.getContextPath()%>
- <%=request.getContextPath()%>的作用
- ${pageContext.request.contextPath}和<%=request.getContextPath()%>
- JSP创建时的<% String path = request.getContextPath();%>
- ${pageContext.request.contextPath}和<%=request.getContextPath()%>