首页 > 代码库 > jsp 页面获取当前路径

jsp 页面获取当前路径

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path+"/";
request.setAttribute("realPath", basePath);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">

function getAppPath(){
return "${realPath}";
}

</script>

1.外联路径

<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/${realPath}css/main.css">

<img src="http://www.mamicode.com/${realPath}images/face01.png" class="img" id="articleAuthor"> <span id="author"></span>

2.在js调用主机地址

var appPath=getAppPath();

$("#playImage").attr("src", appPath+"images/icon_play.png");

 

jsp 页面获取当前路径