在一般的JSP页面中,我们经常可以看到如下代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Date time = new Date();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'demo1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=path%>/ext/ext-all.js"></script>
<link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css">
<script type="text/javascript" src="<%=path%>/demo1.js?time=<%=time%>"></script>
<script type="text/javascript">
Ext.onReady(function () {
Ext.Msg.alert('<%=path%>');
Ext.Msg.alert('<%=basePath%>');
});
</script>
</head> <body> This is my JSP page. <br> </body></html>
以上代码先后弹出以下内容:
/rlsm ----path的值
http://localhost:8080/rlsm/ -----basePath的值
-----------------------------------------------------------------------------------------------------------------------------