jsp及js中获取项目上下文路径
实际使用中,我们发现在JSP中的有时用到 "相对路径",但有时可能会出现问题。多次转发之后的相对路径会发生改变,因为它是相对于 "URL请求地址" 寻找的相对路径。
鉴于上面的情况,总结一下几种方法解决。
1.直接使用绝对路径
<%
//项目上下文路径
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//放入pageContext中
pageContext.setAttribute("basePath",basePath);
%>
使用示例:<html><a href="${pageScope.basePath}jsp/index.jsp"></a></html>
2.c标签设置动态参数(推荐)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:
${pageContext.request.serverPort}${pageContext.request.contextPath}" />
使用示例:<html><a href="${basePath}/jsp/index.jsp"></a></html>
3.js中获取项目上下文路径
function getContextPath() {
var contextPath = document.location.pathname;
var index = contextPath.substr(1).indexOf("/");
contextPath = contextPath.substr(0, index + 1);
delete index;
return contextPath;
}
- 在js,jsp中获取项目绝对路径
- 2017_12_15 js获取项目路径,js调用问题,jsp获取js传递url中参数
- HTML页面使用js获取项目上下文路径
- HTML页面使用js获取项目上下文路径
- js在jsp页面或当独的js文件中获取项目路径
- js获取项目根路径
- JS获取项目根路径
- js获取项目根路径
- js中获取当前项目的根路径
- JSP,JS获得项目路径
- js获取项目根路径
- js获取项目根路径(应用路径方法)
- js获取项目根路径
- js中获取上下文路径
- jsp 获取项目绝对路径
- JS获取当前网址、主机地址项目根路径
- js实现在HTML页面获取项目名路径
- request在js和jsp中获取路径方面的方法
- js获取项目根路径
- jsp获取项目的名字,解决相对路径