50
社区成员
发帖
与我相关
我的任务
分享
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设置一个路径,然后该页面所有相对路径相对于这个路径进行跳转,问题就解决了

还发现一个问题是用火狐启动项目时,有时更改不生效,查看页面源代码发现还是旧代码
解决方法:更换运行的浏览器(其他浏览器发现都可以,就是火狐不变,不知道为啥)

Clear Firefox's cache and try again.