zoukankan      html  css  js  c++  java
  • JavaWeb中的相对绝对路径

    1.什么是绝对路径?

    相对于当前WEB应用的根路径的路径,即任何路径都必须带上contextPath。contextPath可以从request或者application的getContextPath()方法获取。

    2.JavaWeb开发中的“/”到底代表什么?

    (1)若“/”需交由Servlet容器处理,则它代表当前Web应用的根路径:http://localhost:8080/[contextPath]/

    • 请求转发时:request.getRequestDispatcher("/path/c.jsp").forward(request, response);
    • web.xml文件中映射Servlet访问路径:            
    <servlet-mapping>
      <servlet-name>TestServlet</servlet-name>
      <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>
    • 各种定制标签中的 /

    (2)若“/”需交由浏览器处理,则其代表Web站点根路径:http://localhost:8080/

    • 超链接: <a href="/TestServlet">To B page</a>
    • 表单中的action:<form action="/login.jsp" >
    • 做请求重定向的时候:response.sendRedirect("/a.jsp")

    3. jsp/html页面中的路径

    页面中使用路径的常用方式如下:

    (1).当前应用的根路径+静态资源的相对路径: 

    <link type="stylesheet" href="${pageContext.request.contextPath}/satic/ css/common.css">
    <form action="${pageContext.request.contextPath}/servlet/login" method="post">

    浏览器解析时会将”/”解析为服务器。如果不加当前应用的根路径,有可能会错误地把static当作应用名,取决于部署时path的设置。所以最好统一添加应用的根路径。

    获取当前应用的根路径:

    • EL表达式:${pageContext.request.contextPath};  
    • JSP:<% =request.getContextPath() %>,若工程文件就是在根目录下,通过request.getContextPath()返回的字符串为空。

    (2)当前应用的绝对路径+静态资源的相对路径:

    <%@ page language="java" pageEncoding="GBK" contentType="text/html;charset=gbk" isELIgnored="false"%>
    <%
      String appContext= request.getContextPath();// 获取当前应用的根路径构建当前应用的绝对路径(/appName)
      String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + appContext ;
      // 将basePath存入pageContext中,将来用EL表达式读取
      pageContext.setAttribute("basePath",basePath);
    %>
    <html>
    <body>
      <a href="${pageScope.basePath}/jsp/login.jsp">
    </body>
    </html>

    缺点:每个请求前都要加${pageScope.basePath}

    (3)利用<base>标签

    默认情况下,页面中相对路径的请求都是相对于当前页面的url发出的;如果当前页面存在<base>标签,则请求是相对<base>中设定的值。

    <%@ page language="java" pageEncoding="GBK" contentType="text/html;charset=gbk" isELIgnored="false"%>
    <%
    String appContext= request.getContextPath();// 获得当前应用的根路径
    String basePath = request.getScheme()+"://"+request.getServerName()
    + ":" + request.getServerPort() + appContext + "/" ;
    %>
    <html>
    <head>
      <base href="<%=basePath%>"> <!--设定base标签-->
    </head>
    <body>
      <a href="/jsp/login.jsp">Login</a>
    </body>
    </html>

    缺点: 对于被包含的文件依然无效。

    注意:第5行中最后加上了反斜杠,表明给base标签的值是一个目录,否则base标签将不起作用。

  • 相关阅读:
    application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用
    SpringBoot项目打war包部署Tomcat教程
    spring boot 集成 redis lettuce
    spring boot maven打包可运行jar包
    IDEA项目搭建十四——Web站点Controller基类及布局页静态资源设计
    Nginx Windows详细安装部署教程
    多线程编程CompletableFuture与parallelStream
    IDEA项目搭建十三——服务消费端与生产端通信实现
    【异常】MySQL建表报错:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"order"' at line 1
    【警告】mysql链接警告信息:Establishing SSL connection without server's identity verification is not recommended
  • 原文地址:https://www.cnblogs.com/itfky/p/13728298.html
Copyright © 2011-2022 走看看