JavaWeb中路径的写法

来源:互联网 发布:微信裂变源码 编辑:程序博客网 时间:2024/05/21 21:46

结论:开发时候建议编写绝对路径(以"/"开头的路径),相对路径可能会有问题。

何为绝对路径?  带有ContextPath的路径,通常情况下ContextPath=/工程名

获取ContextPath?  request.getContextPath();


路径中[ / ]代表的含义
①WEB应用的根路径:http://localhost:8080/ContextPath/
    >请求转发:request.getRequestDispatcher(path).forward(request, response);
    >web.xml文件映射Servlet访问路径

   <servlet-mapping>
    <servlet-name>ForwordServlet</servlet-name>
    <url-pattern>/forwordServlet</url-pattern>
   </servlet-mapping>

   >各种定制标签

②WEB站点的根路径:http://localhost:8080

   例如:http://localhost:8080/cookiesession/session/session2/login.jsp

   >超链接:<a href="login.jsp"  >切换账号</a> 

                         ==><a href="<%= request.getContextPath() %>/session/session2/login.jsp" >切换账号</a>
   >表单提交中的 action:action=hello.jsp 


   >请求重定向的时候:response.sendRedirect(path);



原创粉丝点击