">对相对路径的影响" /> ">对相对路径的影响 - 秒客网" />

JSP中对相对路径的影响

时间:2022-11-27 15:36:02

如果在JSP中存在

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

和<base href="<%=basePath%>">
,那么在写文件的相对路径的时候,需要把该文件在当前项目下的具体路径写出来。比如JSP中<base href="<%=basePath%>">对相对路径的影响如果jsptest.jsp想访问files/andy.js,则需要写成<a href="json/files/andy.js">Andy</a>,而不是<a href="files/andy.js">Andy</a>。这是因为,<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>的作用是获取当前项目的路径,如:http://localhost:8080/项目名称。

            <base href="<%=basePath%>">是设置基础路径的,basePath为变量,简单的静态网页的话你设置比如:<base href="http://www.baidu.com">,那你下面的href属性就会以你上面设的为基准,如:<a href="http://www.baidu.com/xxx.htm"></a>你现在就只需要写<a href="xxx.htm"></a>