zoukankan      html  css  js  c++  java
  • JavaWeb 的路径访问问题,以及路径命名规则。

    一:路径写法

    1.路径分类:

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

    如:"  ./index.html "
    不以 " / " 开头,以  " . "  开头路径。
    注意:在写路径时, " ./ " 可以省略不写。
    比如:
    
    <a href="./ServletDemo2">  表示当前目录下的ServletDemo2文件
    就可以简写成:
    <a href="ServletDemo2">    表示当前目录下的ServletDemo2文件

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

    " ./  " :当前目录 
    
    " ../ " :后退一级目录
    绝对路径:通过绝对路径可以确定唯一资源,以 " / "开头的路径。
    
    如:http://localhost/day15/responseDemo2  
    
    /day15/responseDemo2

    二:举例子

    在IDEA中:

    我的JavaWeb(JavaEE)项目文件结构:

    我配置了一个虚拟路径:" /Response_war_exploded "。

    所以在Tomcat启动时,默认跳转到这个地址:" http://localhost:8080/Response_war_exploded/  "

    并且打开了  " index.jsp "这个文件页面。

    Web下 "index.jsp" 的访问路径:

    当我们在浏览器输入  http://localhost:8080/Response_war_exploded/index.jsp 时,会发现显示的仍然是 index.jsp 页面,也就是说,jsp 页面的访问路径是直接从项目的 web 目录的下一级目录开始的,我们这里的 index.jsp 由于就是直接处于 web 目录的下一级,所以直接在 localhost:8080/ 后面加上文件名即可。

    所以Web下 "index.jsp" 的访问路径有两个:

    1. http://localhost:8080/Response_war_exploded/index.jsp
    2. http://localhost:8080/Response_war_exploded/

    Web文件夹下其他文件访问路径:

    以 " Location.html " 文件为例,访问路径为:

    http://localhost:8080/Response_war_exploded/Location.html

    src文件夹下的servlet包的Servlet Demo1文件的访问路径:

    这个要看配置:

    servlet 文件的访问路径我们可以在 web.xml 中自己配置(使用的是 url-pattern 标签),或者是使用注解的形式(使用的是 urlPatterns 属性),不管采用哪种形式,其对应的值都和我们的访问路径有关(如果两种形式同时采用,实测只有 web.xml 中的配置生效,也就是说此时如果以注解中配置的路径来访问 servlet 是会报 404 的)
    对于 idea 来说,servlet 的访问路径比较简单,直接在http://localhost:8080/Response_war_exploded/ 后面加上我们自己配置的值即可。

    以我写的ServletDemo1的servlet为例:注解配置为:

    所以它的访问路径为:http://localhost:8080/Response_war_exploded/ServletDemo1。

    怎么配置怎么写,每个人的配置不一样,写法也就不同。

    三.常用写法:

    比如我在Location.html里面写个href:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Location</title>
    </head>
    <body>
            <P>当前资源路径:   http://localhost:8080/Response_war_exploded/Location.html </P>
            <p>目标资源路径:   http://localhost:8080/Response_war_exploded/ServletDemo2</p>
    
            <!--  " ./ " 代表当前目录,  " ../ " 代表当前目录的上一级目录  , " ./ "  可以省略不写 -->
            <a href="ServletDemo2">点击跳转到ServletDemo2</a><br/>
            <a href="./ServletDemo2">点击跳转到ServletDemo2</a><br/>
            <a href="/Response_war_exploded/ServletDemo2">点击跳转到ServletDemo2</a><br/>
    
    
    </body>
    </html>

    上面的三个href的跳转路径写法都能到达目标页面。

    规则:判断定义的路径是给谁用的?判断请求将来从哪儿发出
       * 给客户端浏览器使用:需要加虚拟目录(项目的访问路径)
          * 建议虚拟目录动态获取:request.getContextPath()
          * <a> , <form> 重定向...
       * 给服务器使用:不需要加虚拟目录
          * 转发路径

    参考博客:https://blog.csdn.net/WinstonLau/article/details/80239271

  • 相关阅读:
    ReportingServices报表管理器实现订阅
    sharepoint部署
    在sharepoint工程中添加引用
    asp:DropDownList选择事件出错
    ETL项目
    javascript refresh page 刷新页面的几种方法(转)
    innerHtml 和 innerText用法:(转载)
    用JavaScript隐藏控件的方法(转)
    JS中onpropertychange事件和onchange事件区别
    Window 对象
  • 原文地址:https://www.cnblogs.com/Romantic-Chopin/p/12451024.html
Copyright © 2011-2022 走看看