jsp訪問路徑的解決方法可經過EL表達式、jsp中的java語言所提供的方法、DHTML.js所提供的方法等javascript
如源代碼所示:html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp訪問路徑</title>
</head>
<body onload="load()">
<div>
<h1>jsp訪問路徑的解決方法</h1>
<h2>方法一:經過EL表達式獲取路徑值</h2>
method one(EL):<br>
${header['host']}${pageContext.request.contextPath}
<h2>方法二:經過DHTML.js裏提供的方法(建立javaee項目時,js插件默認已有)</h2>
<p id="p_id"></p>
<h2>方法三:經過jsp中的java語言</h2>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<span>
method three(java):
<br><%=basePath%>
<br><%=path%>
</span>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
function load(){
var curPath = window.document.location.href;
var pathName = window.document.location.pathname;
var port = window.document.location.port;
var hostname = window.document.location.hostname;
var host = window.document.location.host;
$("#p_id").html("method two(js):"+
"<br>curPath:"+curPath+ //訪問當前界面絕對路徑
"<br>pathName:"+pathName+ //從項目名開始算起
"<br>port:"+port+ //tomcat部署的端口號
"<br>hostname:"+hostname + //主機名和端口號
"<br>host:" + host);//主機名
}
</script>
</body>
</html>java
picture:jquery
Methods in DHTML.js:tomcat
其中jsp
/** @type {string} 使用註解聲明string類型 */ Location.prototype.hostname = null; //對應window.document.location.hostname;
注:方法是多種多樣的,無論採用哪一種,只要能解決jsp訪問路徑的問題便可(若要對應本身的項目路徑,建議可經過String所提供的分割或替換的方法處理)ui