jsp文件引用问题

在jSP页面引用文件时常会涉及到一些引用一些js、css等文件,关于如何引用以及引用路径等问题一般采用如下两种方法:

一、采用拼装当前网页的相对路径的方式

        一般在jsp文件中采用如下这样的代码

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <base href="<%=basePath%>">
    <!--Import jquery-->
    <script src="utils/jquery/jquery-3.1.0.min.js"></script>
    <link rel="stylesheet" href="utils/materialize/css/materialize.min.css">
    <script type="text/javascript" src="utils/materialize/js/materialize.min.js"></script>
    <!--Import main.css样式-->
    <script type="text/javascript" src="js/main/index/index.js"></script>
    <link rel="stylesheet" href="css/main/index/index.css">
</head>
<body>
<body>

这个语句是用来拼装当前网页的相对路径的。
<base href="...">是用来表明当前页面的相对路径所使用的根路径的。
比如,页面内部有一个连接,完整的路径应该是 http://localhost:80/myblog/authen/login.do
其中http://server/是服务器的基本路径,myblog是当前应用程序的名字,那么,我的根路径应该是那么http://localhost:80/myblog/。
有了这个 <base ... >以后,我的页面内容的连接,我不想写全路径,我只要写 authen/login.do就可以了。服务器会自动把 <base ...>指定的路径和页面内的相对路径拼装起来,组成完整路径。
如果没有这个 <base...>,那么我页面的连链接就必须写全路径,否则服务器会找不到。
request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http”
request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost"
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80,
request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的myblog
这四个拼装起来,就是当前应用的跟路径了

、采用EL表达式   ${pageContext.request.contextPath }



<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*"%>
<html>
<head>
    <!--Import jquery-->
    <script src=" ${pageContext.request.contextPath }/utils/jquery/jquery-3.1.0.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath }/utils/materialize/css/materialize.min.css">
    <script type="text/javascript" src="${pageContext.request.contextPath }/utils/materialize/js/materialize.min.js"></script>
    <!--Import main.css样式-->
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/main/index/index.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath }/css/main/index/index.css">
</head>
<body>

注意

${pageContext.request.contextPath}样是通过 get方法去取的,先pageContext.getRequest()得到HttpServletRequest对象,再调用 HttpServletRequest的getContextPath方法

作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的。

El表达式的写法:${pageContext.request.contextPath}

jsp的写法:<%=request.getContextPath()%>


${pageContext.request.contextPath}等价于<%=request.getContextPath()%> 或者可以说是<%=request.getContextPath()%>的EL版 意思就是取出部署的应用程序名或者是当前的项目名称

比如我的项目名称是ajax01 在浏览器中输入为http://localhost:8080/ajax01/login.jsp ${pageContext.request.contextPath}或<%=request.getContextPath()%>取出来的就是/ajax01,而"/"代表的含义就是http://localhost:8080

所以我们项目中应该这样写${pageContext.request.contextPath}/login.jsp





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值