• web工程中URL地址的推荐写法


    三、web工程中URL地址的推荐写法

    使用c标签<c:url value="" /> 会自动添加项目名 -> value中的值 前面要加 “/”

      在JavaWeb开发中,只要是写URL地址,那么建议最好以"/"开头,也就是使用绝对路径的方式,那么这个"/"到底代表什么呢?可以用如下的方式来记忆"/":如果"/"是给服务器用的,则代表当前的web工程,如果"/"是给浏览器用的,则代表webapps目录。

    3.1、"/"代表当前web工程的常见应用场景

    ①.ServletContext.getRealPath(String path)获取资源的绝对路径

    复制代码
    1 /**
    2 * 1.ServletContext.getRealPath("/download/1.JPG")是用来获取服务器上的某个资源,
    3 * 那么这个"/"就是给服务器用的,"/"此时代表的就是web工程
    4  * ServletContext.getRealPath("/download/1.JPG")表示的就是读取web工程下的download文件夹中的1.JPG这个资源
    5 * 只要明白了"/"代表的具体含义,就可以很快写出要访问的web资源的绝对路径
    6 */
    7 this.getServletContext().getRealPath("/download/1.JPG");
    复制代码

    ②.在服务器端forward到其他页面

    1 /**
    2 * 2.forward
    3  * 客户端请求某个web资源,服务器跳转到另外一个web资源,这个forward也是给服务器用的,
    4 * 那么这个"/"就是给服务器用的,所以此时"/"代表的就是web工程
    5 */
    6 this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);

    ③.使用include指令或者<jsp:include>标签引入页面

    1 <%@include file="/jspfragments/head.jspf" %>
    1 <jsp:include page="/jspfragments/demo.jsp" />

      此时"/"代表的都是web工程。

    3.2、"/"代表webapps目录的常见应用场景

    ①.使用sendRedirect实现请求重定向

    1 response.sendRedirect("/JavaWeb_HttpServletResponse_Study_20140615/index.jsp");

      服务器发送一个URL地址给浏览器,浏览器拿到URL地址之后,再去请求服务器,所以这个"/"是给浏览器使用的,此时"/"代表的就是webapps目录,"/JavaWeb_HttpServletResponse_Study_20140615/index.jsp"这个地址指的就是"webappsJavaWeb_HttpServletResponse_Study_20140615index.jsp"

      response.sendRedirect("/项目名称/文件夹目录/页面");这种写法是将项目名称写死在程序中的做法,不灵活,万一哪天项目名称变了,此时就得改程序,所以推荐使用下面的灵活写法:

    1 response.sendRedirect("/JavaWeb_HttpServletResponse_Study_20140615/index.jsp");

      这种写法改成

    1 response.sendRedirect(request.getContextPath()+"/index.jsp");

      request.getContextPath()获取到的内容就是"/JavaWeb_HttpServletResponse_Study_20140615",这样就比较灵活了,使用request.getContextPath()代替"/项目名称",推荐使用这种方式,灵活方便!

    ②.使用超链接跳转

    1 <a href="/JavaWeb_HttpServletResponse_Study_20140615/index.jsp">跳转到首页</a>

      这是客户端浏览器使用的超链接跳转,这个"/"是给浏览器使用的,此时"/"代表的就是webapps目录。

      使用超链接访问web资源,绝对路径的写法推荐使用下面的写法改进:

    1 <a href="${pageContext.request.contextPath}/index.jsp">跳转到首页</a>

      这样就可以避免在路径中出现项目的名称,使用${pageContext.request.contextPath}取代"/JavaWeb_HttpServletResponse_Study_20140615"

    ③.Form表单提交

    1 <form action="/JavaWeb_HttpServletResponse_Study_20140615/servlet/CheckServlet" method="post">    
    2         <input type="submit" value="提交">
    3 </form>

      这是客户端浏览器将form表单提交到服务器,所以这个"/"是给浏览器使用的,此时"/"代表的就是webapps目录。

     对于form表单提交中action属性绝对路径的写法,也推荐使用如下的方式改进:

    1 <form action="${pageContext.request.contextPath}/servlet/CheckServlet" method="post">
    2          <input type="submit" value="提交">
    3 </form>

      ${pageContext.request.contextPath}得到的就是"/JavaWeb_HttpServletResponse_Study_20140615"

      ${pageContext.request.contextPath}的效果等同于request.getContextPath(),两者获取到的都是"/项目名称"

    ④.js脚本和css样式文件的引用

    1  <%--使用绝对路径的方式引用js脚本--%>
    2  <script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>
    3  <%--${pageContext.request.contextPath}与request.getContextPath()写法是得到的效果是一样的--%>
    4  <script type="text/javascript" src="<%=request.getContextPath()%>/js/login.js"></script>
    5  <%--使用绝对路径的方式引用css样式--%>
    6  <link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css" type="text/css"/>

    综合范例:

    复制代码
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 
     3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     4 <html>
     5   <head>
     6     <title>"/"代表webapps目录的常见应用场景</title>
     7     <%--使用绝对路径的方式引用js脚本--%>
     8     <script type="text/javascript" src="${pageContext.request.contextPath}/js/index.js"></script>
     9     <%--${pageContext.request.contextPath}与request.getContextPath()写法是得到的效果是一样的--%>
    10     <script type="text/javascript" src="<%=request.getContextPath()%>/js/login.js"></script>
    11     <%--使用绝对路径的方式引用css样式--%>
    12       <link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css" type="text/css"/>
    13   </head>
    14   
    15   <body>
    16       <%--form表单提交--%>
    17        <form action="${pageContext.request.contextPath}/servlet/CheckServlet" method="post">
    18            <input type="submit" value="提交">
    19        </form>
    20        <%--超链接跳转页面--%>
    21        <a href="${pageContext.request.contextPath}/index.jsp">跳转到首页</a>
    22   </body>
    23 </html>
    复制代码

     

  • 相关阅读:
    HTTP请求报文与响应报文
    RTTI (Run-time type information) in C++
    Advanced C++ | Virtual Copy Constructor
    Virtual Destructor
    Advanced C++ | Virtual Constructor
    What happens when more restrictive access is given to a derived class method in C++?
    Can static functions be virtual in C++?
    Virtual functions in derived classes
    Default arguments and virtual function
    Multiple Inheritance in C++
  • 原文地址:https://www.cnblogs.com/lyc-smile/p/5112901.html
Copyright © 2020-2023  润新知