JavaWeb15.2【response:Response对象功能、案例-重定向、绝对路径相对路径】
1 package com.haifei.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 /** 11 * Response对象案例-重定向 12 */ 13 @WebServlet("/responseDemo1") 14 public class ResponseDemo1 extends HttpServlet { 15 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 16 System.out.println("demo1被访问了。。。"); 17 18 //访问/responseDemo1资源,会自动跳转到/responseDemo2资源 19 /*//1 设置状态码为302 20 response.setStatus(302); 21 //2.设置响应头location 22 response.setHeader("location", "/day15/responseDemo2");*/ 23 /* 24 http://localhost:8080/day15/responseDemo1 25 26 demo1被访问了。。。 27 demo2被访问了。。。 28 */ 29 30 request.setAttribute("msg", "response"); //存值 31 32 // //有一种简单的重定向方法,可以实现上述功能 33 // response.sendRedirect("/day15/responseDemo2"); 34 //// response.sendRedirect("http://www.bilibili.com"); //重定向可以访问其他站点(服务器)的资源 35 36 //此外,"/day15/responseDemo2"不方便,虚拟目录可能更改频繁,且容易写错虚拟目录 37 //动态获取虚拟目录 38 String contextPath = request.getContextPath(); 39 response.sendRedirect( contextPath + "/responseDemo2"); 40 } 41 42 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 43 this.doPost(request, response); 44 } 45 }
1 package com.haifei.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 @WebServlet("/responseDemo2") 11 public class ResponseDemo2 extends HttpServlet { 12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 13 System.out.println("demo2被访问了。。。"); 14 15 /*Object msg = request.getAttribute("msg");//取值 16 System.out.println(msg); //null --> 获取不到数据*/ 17 } 18 19 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 this.doPost(request, response); 21 } 22 }
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>相对路径规则:找到当前资源和目标资源之间的相对位置关系</h1> 9 <P> 10 当前资源: 11 http://localhost:8080/day15/location.html 12 </P> 13 <P> 14 目标资源: 15 http://localhost:8080/day15/responseDemo2 16 </P> 17 <a href="./responseDemo2">responseDemo2</a> <!--相对路径中./表示当前位置,且./可以省略--> 18 <a href="responseDemo2">responseDemo2</a> <!--相对路径中./表示当前位置,且./可以省略--> 19 20 21 <h1>绝对路径规则:判断定义的路径是给谁用的?即判断请求将来从哪儿发出</h1> 22 <a href="/day15/responseDemo2"> <!--绝对路径给客户端浏览器使用:需要加虚拟目录(项目的访问路径)--> 23 responseDemo2 24 </a> 25 </body> 26 </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>找到当前资源和目标资源之间的相对位置关系</h1> <P> 当前资源: http://localhost:8080/day15/htmls/location2.html </P> <P> 目标资源: http://localhost:8080/day15/responseDemo2 </P> <a href="../responseDemo2">responseDemo2</a> <!--相对路径中../表示后退一级目录,且../不可省略--> </body> </html>
1 package com.haifei.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 @WebServlet("/responseDemo3") 11 public class ResponseDemo3 extends HttpServlet { 12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 13 //绝对路径举例 14 15 //转发 16 request.getRequestDispatcher("/responseDemo2").forward(request, response); //绝对路径给服务器使用:不需要加虚拟目录 17 /* 18 http://localhost:8080/day15/responseDemo3 19 20 demo2被访问了。。。 21 */ 22 } 23 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 this.doPost(request, response); 26 } 27 }