JSP中使用html中的<base>标签不起作用

html中的引用路径确实是个问题。比如引用一张图片吧,由于图片需要分门别类吧。所以图片放到不同的文件夹中,有可能的话,会嵌套文件夹,所以引用时,需要一层层滴寻找。

常规的做法是一层层回退到根目录下,然后在一层层递增寻找图片。常规代码:

<img src="../images/mark_arrow_03.gif" width="14" height="14">

若是嵌套的文件夹个数多的话,甚至代码如此:

<img src="../../images/mark_arrow_03.gif" width="14" height="14">

若按照上述做法,确实不方便,甚至会出现错误,少回退一步:../. 为了解决这种问题,我们使用html中的base标签,即从根目录下寻找。

<head> <base href="<%=basePath%>"> </head>

根目录路径即为:

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

当时为了测试根目录路径问题,在界面上打印根目录路径:

out.println(basePath);

但是JSP中的引用的图片、引用的JS,一切都不起作用。

当时以为是IE浏览器的原因,调试了半天。因为一般情况调试JSP界面,习惯使用out.println(),一点点测试,但是最后调试以及上网寻找原因,竟然原因在于out.println()。

得出的结论:若是在html头部打印其他字符,那么<html>中的base标签不会起作用。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'main_left.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"> --> <style> body { font-size: 35px; /* 设置字体大小 */ text-align: center; /* 设置文本居中对齐 */ background-color: black; color: white; font-family: Arial, sans-serif; } a { color: white; text-decoration: none; margin: 10px; padding: 10px; } </style> </style> </head> <body> <br><br> <a href="inputStuInfo.jsp" target="main_right">录入学生信息</a><br><br> <a href="student/queryAllStuServlet?osid=query&page=1" target="main_right">查看学生信息</a><br><br> <a href="student/queryAllStuServlet?osid=modify&page=1" target="main_right">修改学生信息</a><br><br> <a href="student/queryAllStuServlet?osid=delete&page=1" target="main_right">删除学生信息</a><br><br> <a href="user/exitServlet" target="_top">退出</a><br> </body> </html> 解释代码
最新发布
05-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值