Response_路径_相对路径以及绝对路径

Response_路径_相对路径以及绝对路径

1.相对路径:通过相对路径不可以确定唯一资源

  如:./index.html

  不以/开头,以.开头路径

 

规则:找到当前资源和目标资源之间的相对位置关系

  ./当前路径

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>找到当前资源和目标资源的相对位置关系</h1>
    <p>
        当前资源:location.html
        http://localhost/location.html
    </p>
    <p>
        目标资源:
        http://localhost/responseDemo2
    </p>

    <a href="./responseDemo2">
        responseDemo02
    </a>
</body>
</html>
复制代码

  ../:回退以及目录

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>找到当前资源和目标资源的相对位置关系</h1>
    <p>
        当前资源:location2.html
        http://localhost/htmls/location2.html
    </p>
    <p>
        目标资源:
        http://localhost/responseDemo2
    </p>

    <a href="../responseDemo2">
        responseDemo02
    </a>
</body>
</html>
复制代码

 

 

2绝对路径:通过绝对路径可以确定唯一资源

  如:http://localhost/responseDemo2    

  以/开头路径

html页面:

复制代码
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <h1>绝对路径</h1>
    <a href="/requestDemo2">
        requestDemo2
    </a>
</body>
</html>
复制代码
复制代码

 

内部使用:

复制代码
复制代码
@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //转发
        request.getRequestDispatcher("/responseDemo2").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码
复制代码

 

 

规则:判断定义的路径是给谁用的?判断请求将来从哪发出

  给客户端浏览器使用:需要加虚拟目录(项目的访问路径)

    建议虚拟目录动态获取:request.getContextPath()

 

  给服务器使用:不需要加虚拟目录

 

posted @   monkey大佬  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示