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()      返回当前页面所在的应用的名字;

 













































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值