zoukankan      html  css  js  c++  java
  • web工程中URL地址的推荐写法

    在Javaweb开发中,只要写URL地址,建议以“/”开头,也就是使用绝对路径的方式。

    “/”:如果是给服务器的,代表当前的web工程。给浏览器的,代表webapps目录

    代表web工程

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

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

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

    ③.使用include指令或者<jsp:include>标签引入页面
    <jsp:include page="/jspfragments/demo.jsp" />
    <%@include file="/jspfragments/head.jspf" %> 代表的都是web工程
    代表webapps目录
    ①.使用sendRedirect实现请求重定向
    使用request.getContextPath()代替"/项目名称",推荐使用这种方式,灵活方便!
    ②.使用超链接跳转
    推荐使用:<a href="${pageContext.request.contextPath}/index.jsp">跳转到首页</a>
    ③.Form表单提交
    1 <form action="/JavaWeb_HttpServletResponse_Study_20140615/servlet/CheckServlet" method="post">    
    2         <input type="submit" value="提交">
    3 </form>
    
    
    改进:
    1 <form action="${pageContext.request.contextPath}/servlet/CheckServlet" method="post"> 2 <input type="submit" value="提交"> 3 </form>
     ${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"/>




  • 相关阅读:
    python之路----初识面向对象(二)
    python之路----初识面向对象
    python之路----包
    python之路----模块与序列化模块
    python之路----模块调用
    python之路----常用模块二
    Python之路----递归函数
    Python之路----内置函数
    【CSS】整屏大背景
    【PHP】打印输出var_dump+echo+print_r
  • 原文地址:https://www.cnblogs.com/bulrush/p/5667861.html
Copyright © 2011-2022 走看看