JavaEE路径


相对路径与绝对路径

相对路径与绝对路径的区别
绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)。例如:C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm 代表了一个URL绝对路径。但是在web项目中,/...这种就是绝对路径(URI),因为'/'代表Web应用的根目录。

相对路径:相对与某个基准目录的路径。例如:"./"代表当前目录,"../"代表上级目录。(在同一路径下时, ./login.jsp==login.jsp)。在web项目中,.../.../...这种就是相对路径。

在服务器端(jsp和servlet)中,根目录是web应用的地址,如:http://localhost:8080/tmall/ ,所以写绝对路径时直接为/...;在客户端(html)中,根目录是服务器根目录的,如:http://localhost:8080/ ,所以写绝对路径是应为/tmall/...
A/B/C与A/D :C访问D时,相对地址D;D访问C时,相对地址B/C。(不懂为啥这样)

jsp,servlet中的路径

注意jsp,servlet中的各个参数是要path还是要URI还是URL。

1. servlet配置url
servlet的url-pattern匹配规则
两种方式:

@WebServlet("/loginServlet")//简写

<!-- servlet的映射配置 -->
<servlet-mapping>
    <!-- servlet的映射路径(访问servlet的名称) -->
    <url-pattern>/loginServlet</url-pattern>
</servlet-mapping>

Servlet类只能交给tomcat服务器运行,因此在获得url请求后,url会转化为可映射匹配的路径。

2. servlet forward

request.getRequestDispatcher("/userServlet").forward(request, response); 
request.getRequestDispatcher("/login.jsp").forward(request, response); 
//可以看出都是在根路径下,所以相当于
request.getRequestDispatcher("userServlet").forward(request, response); 
request.getRequestDispatcher("login.jsp").forward(request, response); 
//或
request.getRequestDispatcher("./userServlet").forward(request, response); 
request.getRequestDispatcher("./login.jsp").forward(request, response); 

问题1:为什么servlet不能直接写URL?如request.getRequestDispatcher("http://localhost:8080/tmall/login.jsp").forward(request, response);
问题2:上述报错404,消息:/tmall/http://localhost:8080/tmall/login.jsp
报错的路径为啥自动加上根路径呢?

问题1解答:request.getRequestDispatcher()有这样的介绍:
参数类型:RequestDispatcher getRequestDispatcher(String path)
path介绍:The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root.
也就是说,getRequestDispatcher中的参数类型必须是路径。而url不是路径,url是网络地址。在http中,URL表示为http://< host >:< port >/< path >。也就是说,例子中tmall/login.jsp才是路径。

forward这里的path必须是同一个web应用,不能跳转至其他应用界面,效率较高。它与response的重定向不同,重定向不会保存请求信息,相当于重新输入url(类似于超链接)。而forward是用目标页面的内容代替原来的页面内容,会保留请求信息。

这又产生了新的问题:path是不是可以写为tmall/login.jsp或者/tmall/login.jsp
答案:不能。tmall/login.jsp被解析为相对路径,那么根路径已经为/tmall;/tmall/login.jsp中第一个'/'还是被解析为根路径,所以两者的路径都会被理解为/tmall/tmall/login.jsp。

问题2解答:被理解为了相对地址,所以这样。

解答都是自己猜的 O(∩_∩)O

3. 得到路径

假定你的web application 名称为tmall,你在浏览器中输入请求路径:
http://localhost:8080/tmall/admin/index.jsp
则执行下面向行代码后打印出如下结果:
System.out.println(request.getContextPath());//获取项目的根路径
打印结果:/tmall
System.out.println(request.getServletPath());//获取能够与“url-pattern”中匹配的路径
打印结果:/admin/index.jsp
System.out.println(request.getRequestURI());//获取根路径到地址结尾
打印结果:/tmall/admin/index.jsp
System.out.println(request.getRequestURL());//获取请求的地址链接(浏览器中输入的地址)
打印结果:http://localhost:8080/tmall/admin/index.jsp

4. URI与URL
URL和URI的区别
简单来说,URL是URI的一种。
在form action中,我们可以

//URL方式
<form class="loginForm" action="http://localhost:8080/tmall/loginServlet" method="post">
//URI方式 绝对路径
<form class="loginForm" action="/tmall/loginServlet" method="post">
//URI方式 相对路径
<form class="loginForm" action="loginServlet" method="post">

5. 其他

  • jsp的标识符
    action -> Attribute : action ; Data Type : URI
    file -> Attribute : file ; Data Type : URI

  • 文件路径
    如:D:workspace.metadatawtpwebapps mall,方向不一样。
    因为在字符串中存在转译的问题,所以:"D:\workspace\metadata\wtpwebapps\tmall"。

  • 关于import
    import 的作用以及import与#include区别
    刚开始还以为import后面也是某种地址,后面才知道是包名和类名(只有public类和接口才能被导入)。

  • WebContent下面的文件的上级目录是项目名,没有WebContent这一级。如:tmall/index.jsp。
    据老师说,是因为export->war->rar->解压后会发现文件路径是这样。

  • cookie与session
    1)cookie把客户数据保存在客户端浏览器,不太安全;
    通过response传递:response.addCookie(new Cookie(...));
    通过request.getCookies()获得cookie数组;
    使用时必须设置生存周期,否则会随着浏览器的关闭而消失。
    2)session把客户信息保存在服务器;
    可以通过request.getSession()获得请求的session对象;
    可以用session.setAttribute()和session.getAttribute()方法传递信息;
    一次会话指从客户端浏览器与服务器连接开始到断开(用户关闭浏览器)为止。
    浅谈Session与Cookie的关系
    session与cookie之间的关系

原文地址:https://www.cnblogs.com/massizhi/p/12687775.html