JSP中path与basePath的区别

在一般的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的值

-----------------------------------------------------------------------------------------------------------------------------

知识点:

      ① request.getScheme()            

返回当前页面使用的协议;默认返回http,SSL时返回https;

      ② request.getServerName()    

 返回当前页面所在的服务器的名字;

      ③request.getServerPort()       

 返回当前页面所在的服务器使用的端口,就是项目在服务器上发布的 端口,或者在本地tomcat容器运行时发布的端口,我用的是8080;

     ④request.getContextPath()    

 返回当前页面所在的应用的名字;

猜你喜欢

转载自blog.csdn.net/qq_34266804/article/details/89022318