1. "/" 使用的三个最重要的绝对路径。
1. web.xml中使用“/”,给servlet指定url-partner是为servlet指定通过什么路径能够访问到当
前的servlet。
```
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
比如我们设置“/test”,那其实我们需要通过localhost:8080/projectname(项目名称)/test
才能够访问到servlet,所以这里的/表示的是”localhost:8080/projectname(项目名称)/“。
“/"可以理解为一个缩写。
2. 转发和重定向他们的路径都可以是相对路径,如果是相对路径,在转发和重定向中都一样。
转发中“/”表示:localhost:8080/projectname(项目名称)/,
重定向中使用“/”表示:localhost:8080/,也就是到达webapp。
3. 在HTML中绝对路径“/”表示的是到达tomcat的webapp,不包括当前项目路径,
只要是HTML前端的“/”到达的都是:localhost:8080"。
小结一下:
在可以跨域的地方“/”表示的是:localhost:8080
如果不能跨域的地方表示的就是:localhost:8080/projectname(项目名称)/。
1. a标签:<a href="/day53/index.jsp">跳转</a> 的路径如果是"/"开头,
"/"则相当于"localhost:8080"。
2. form表单:<form action= "/"></form>
3. <script type="text/javascript" src="/"></script>
4. <link rel="stylesheet" type="text/css" href="/" />
5. <script type="text/javascript" >
window.location.href="/"</script>
以上"/"都表示:localhost:8080
1 请求转发----request
1.请求转发:一种在服务器内部的资源跳转的方式
如果实现一个需求需要多个Servlet,比如day0428项目下有AServlet和BServlet。
AServlet实现一部分后,BServlet实现后面的一部分。那么浏览器访问AServlet后要
跳转到BServlet。这就是跳转的一种方式。
1. 步骤:
1. 通过request对象获取请求转发器对象:
RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象进行转发:
forward(ServletRequest request, ServletResponse response)
2. 特点:
1. 资源虽然跳转了,但是浏览器地址栏路径没有发生变化
2. 转发只能跳转到当前服务器内部的资源
3. 转发只发送了一次请求(虽然访问了两个资源)。即多个资源使用了一个请求。
2 服务器重定向---response
1.完成重定向
*重定向和转发类似,也是一种资源跳转的方式。
重定向发生在:
A资源告诉浏览器,我没有满足你的资源,B资源有。
A资源告诉浏览器重定向状态码:302;
告诉B资源的路径:响应头location(B资源的路径)
* 代码
```
//访问/responseDemo1资源,会自动跳转到/responseDemo2资源。
//1.设置状态吗为302
resp.setStatus(302);
//2.设置响应头location
resp.setHeader("location", "/day0504_Response/responseDemo2");
```
* 发现除了setHeader()的第二个参数外,其他都是固定的,所以Response对象提供了
简单的重定向方式。
void sendRedirect(String location)
resp.sendRedirect("/day0504_Response/responseDemo2");
* 为了避免虚拟目录改变引起的资源丢失:动态获取虚拟目录
String contextPath = req.getContextPath();
resp.sendRedirect(contextPath+"/responseDemo2");
* 重定向(redirct)的特点:
1. 重定向地址栏路径变化
2. 重定向可以访问其他站点(服务器)的资源
3. 重定向是两次请求:所以是两次request,两个request域
不能request对象来共享数据了。
* 转发(forward)的特点:
1. 转发地址栏路径不变
2. 转发只能访问当前服务器下的资源
3. 转发是一次请求。
意味着可以使用request对象来共享数据
3 共享数据
* 域对象:一个有作用范围的对象,可以在范围内共享数据。
1 request域:代表了一次请求的范围。一般用于请求转发的多个资源中共享数据。
* 方法:
1. serAttribute(String name, Object obj):存储数据(键值对形式)
2. getAttribute(String name): 通过键获取值
3. removeAttribute(String name) : 通过建来移除键值对。
2 ServletContext对象
1.概念:代表整个web应用(工程),可以和应用的容器(服务器)来通信
2.获取:
1.通过request对象获取
request.getServletContext();
2.通过HttpServlet获取
HttpServlet的父类GenericServlet中的方法:getServletContext()
一般我们写的Servlet继承了HttpServlet,所以可以直接用。
this.getServletContext();
3.功能:
1.获取MIME类型
* MINE类型:在互联网通信过程中定义的一种文件数据类型
* 格式:大类型/小类型 text/html image/jpeg
* 获取:通过ServletContext中的方法:
* String getMimeType(String file)
2.域对象:共享数据的。
1. setAttribute(String name, Object value)
2. getAttribute(String name)
3. removeAttribute(String name)
* ServletContext对象域的范围:所有用户(可以看做不同的的浏览器,
那么就是不同用户的请求),所有请求的数据。
* 生命周期:服务器启动创建,服务器关掉销毁。所以在内存中长时间
存在。
3 session
1. session用于存储一次会话的多次请求的数据,存在服务器端
一次会话间的数据可以用session共享。会话域
2. session可以存储任意类型,任意大小的数据。request的cookie中只能是字
符串(request域),而且大小有限制。
4 转发和重定向的分析
一、请求转发与响应重定向的种类
有两种方式获得Servlet转发对象(RequestDispatcher):
1. HttpServletRequest的getRequestDispatcher()方法获得
2. ServletContext的getRequestDispatcher()方法获得
Servlet重定向的方法只有一种:
HttpServletResponse的sendRedirect()方法。
这三个方法的参数都是一个URL形式的字符串,但在使用相对路径或绝对路径上有所区别。
5 请求转发与响应重定向中路径参数区别
5.1 响应重定向中路径参数
绝对路径: / 开头
相对路径: 不是/开头
背景:
1.http://localhost:8080/day0615_springmvc01_response/response.jsp
开始页面
```
<a href="user/testVoidResponse">testVoidResponse</a>
```
2. 点击链接到达控制器CobtrollerServlet中的方法:
http://localhost:8080/day0615_springmvc01_response/user/testVoidResponse
```
@RequestMapping("/testVoidResponse")
public void testVoid(HttpServletRequest request, HttpServletResponse response)
throws Exception{
System.out.println("testVoid方法执行了...");
response.sendRedirect("../index.jsp");
System.out.println(request.getContextPath());
return;
}
```
A:方法重定向相对路径参数: ../index.jsp
容器(Tomcat)相对于原来请求的URL的目录+参数来构成完整的URL
明确所在当前目录:
http://localhost:8080/day0615_springmvc01_response/user/
参数1:./index.jsp
会发送新访问的请求:跳到当前目录下+ /index.jsp
http://localhost:8080/day0615_springmvc01_response/user/index.jsp
参数2:../index.jsp
会发送新访问的请求:向上跳1级
http://localhost:8080/day0615_springmvc01_response/index.jsp‘’
参数2等价于绝对路径:request.getContextPath+"index.jsp"
参数3: index.jsp
会发送新访问的请求:相对于当前目录 + /index.jsp
http://localhost:8080/day0615_springmvc01_response/user/index.jsp
参数3等价于参数1
B:方法重定向绝对路径写法:--- 绝对路径/开头
容器(Tomcat)相对于容器本身localhost:8080/+参数构建完整的URL
因为重定向是在次发送一个新的请求,有客户端在请求一次服务器,请求是在服务器外进行的。
1.参数1:/index.jsp
发送的新的访问请求:http://localhost:8080/index.jsp
2.参数2:/day0615_springmvc01_response/index.jsp
发送的新的访问请求:
http://localhost:8080/day0615_springmvc01_response/index.jsp
5.2 请求转发中路径参数
1.开始页面
<a href="user/testVoidRequest">testVoidRequest</a>
2. 点击链接到达控制器CobtrollerServlet中的方法:
```
@RequestMapping("/testVoidRequest")
public void testVoidRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception{
System.out.println("testVoid方法执行了...");
request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
return;
}
```
A: 请求转发参数:相对路径
同重定向:容器(Tomcat)相对于原来请求的URL的目录+参数来构成完整的URL
原来请求的URL的目录:http://localhost:8080/day0615_springmvc01_response/user/
参数1:../WEB-INF/pages/success.jsp
转发路径:http://localhost:8080/day0615_springmvc01_response/WEB-INF/pages/success.jsp
参数2:success.jsp
转发路径:http://localhost:8080/day0615_springmvc01_response/user/success.jsp
B:请求转发参数:绝对路径
容器将相对于Web应用的根目录加参数生成完整的URL。即"/"根路径就是虚拟路径,当前项目的根路
径。因为转发是在服务器内部进行的,写绝对路径开头“/”指的当前web项目。
当前项目根目录:http://localhost:8080/day0615_springmvc01_response/
参数1. /WEB-INF/pages/success.jsp
转发路径:http://localhost:8080/day0615_springmvc01_response/WEB-INF/pages/success.jsp
参数2:/success.jsp
转发路径:http://localhost:8080/day0615_springmvc01_response/success.jsp
总结:
1.重定向的 / 表示:http://服务器ip:端口/
2.请求转发的 / 表示:http://服务器ip:端口/项目名/
3.相对路径规则相同。
重定向是浏览器发出的,只知道发到哪个服务器,但是不知道发到服务器的哪个project,所以/表示
http://服务器ip:端口/。
转发是服务器某个project内部的转发,都是在服务器内部,所以/表示:
http://服务器ip:端口/项目名/