jsp文件引用问题
来源:互联网 发布:电脑安全上网软件 编辑:程序博客网 时间:2024/06/02 04:52
在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
<%@ 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>这个语句是用来拼装当前网页的相对路径的。
<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
这四个拼装起来,就是当前应用的跟路径了
<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
- jsp文件引用问题
- JSP引用JS文件中文乱码问题
- jsp引用css文件
- SpringMVC hibernate整合时jsp中引用文件问题
- JSP文件引用其他文件
- 引用外部.css或.js文件的路径问题--jsp或html页面引用
- JSP引用外部js文件
- jsp中js文件引用
- 引用文件路径问题
- 引用外部.css或.js文件的路径问题--jsp或html页面或是在web项目中的引用
- 关于WEB-INF目录不提供外部访问及JSP引用 js,css 文件路径问题
- jsp文件上传问题
- 在jsp中引用js文件无效
- js文件引用JSP EL vo
- JSP中引用图片路径问题
- JSP中引用javascript乱码问题
- JSP中引用javascript乱码问题
- Jsp和Servlet中引用路径问题
- C++中变量的生存周期
- 牛客前端技能挑战
- 剑指offer(C++)——按之字形顺序打印二叉树
- 计算机网路基础
- CMake与Make
- jsp文件引用问题
- 顺时针打印矩阵
- 慕课BIT嵩老师爬虫课笔记
- 7.Unity3D商业游戏源码研究-变身吧主公-SceneLoding
- 数据模型
- 03 Opencv部分经典例程赏析
- [bzoj]1042: [HAOI2008]硬币购物
- BZOJ 2742: [HEOI2012]Akai的数学作业
- 交换两个变量的值的swap函数