Servlet-html-base标签

问题引入

页面二使用a标签请求转发方式跳转页面一,

页面一再使用a标签相对路径的跳转方式跳转到页面二就会出错。

 

 

解决方法:使用base标签

<!-- base标签设置页面相对路径工作时参照的地址
   herf属性就是参数的地址值
   -->
<base href="http://localhost:8088/HtmlBase_war_exploded/a/b/c.html">

相关代码

package com.orz;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author orz
 * @create 2020-09-10 23:00
 */
public class ForwardC extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.getRequestDispatcher("/a/b/c.html").forward(req,resp);
    }
}
View Code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是首页</title>

</head>
<body>
<p>这是web下的index首页</p>
<a href="a/b/c.html">a/b/c.html</a><br/>
<a href="http://localhost:8088/HtmlBase_war_exploded/forwardC">请求转发到a/b/c.html</a>
</body>
</html>
View Code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- base标签设置页面相对路径工作时参照的地址
       herf属性就是参数的地址值
       -->
    <base href="http://localhost:8088/HtmlBase_war_exploded/a/b/c.html">
</head>
<body>
<p>这是a下的b下的c</p>
<a href="../../index.html">返回首页</a>
</body>
</html>
View Code

 

posted @ 2020-09-10 23:18  orz江小鱼  阅读(230)  评论(0编辑  收藏  举报