servlet中base标签的作用

张悦 荣耀王者 2022-09-23 11:44:04

index.html

<!DOCTYPE html>
<html lang="zh_CN">
<head>
  <meta charset="UTF-8">
  <title>首页</title>

</head>
<body>
<!--http://localhost:8080/demo919/index.html-->
这是Web下的index.html <br/>
<a href="a/b/g.html">a/b/g.html</a><br/>
<!--http://localhost:8080/demo919/a/b/c.html-->
<!--../../index.html-->
<!--http://localhost:8080/demo919/index.html-->
<a href="http://localhost:8080/demo919/ForwardC">请求转发:a/b/c.html</a><br/>
<!--http://localhost:8080/demo919/ForwardC-->
<!--http://localhost:8080/index.html-->

<a href="/">斜杠</a>
</body>
</html>

 

 

g.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--  base标签设置相对路径工作时参照的地址-->
    <base href="http://localhost:8080/demo919/a/b/g.html">
</head>
<body>
这里是g
<a href="../../index.html">跳回首页</a>
</body>
</html>

 

ForwardC

package com.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ForwardC extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("经过了ForwardC程序");
        req.getRequestDispatcher("/a/b/g.html").forward(req, resp);
    }
}

 

在跳转过程中发现第二个利用请求跳转没有成功

大概原因就是请求跳转时地址栏的地址是不变的,当再用相对路径进行跳转时就会出现问题,所以用base设置一个路径,然后该页面所有相对路径相对于这个路径进行跳转,问题就解决了

 

 

还发现一个问题是用火狐启动项目时,有时更改不生效,查看页面源代码发现还是旧代码

解决方法:更换运行的浏览器(其他浏览器发现都可以,就是火狐不变,不知道为啥)

 

...全文
31 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yolanda19910002 荣耀王者 2022-09-24
  • 打赏
  • 举报
回复

Clear Firefox's cache and try again.

50

社区成员

发帖
与我相关
我的任务
社区描述
产学研究,学术传承
社区管理员
  • 又菜又爱玩呜呜呜~
  • 两年半的个人练习生^_^
  • yolanda19910002
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧