2022.5.8 请求重定向与转发 base标签

请求重定向

请求重定向,是指客户端给服务器发请求,然后服务器告诉客户端去新地址访问。叫请求重定向(之前的地址可能已经被废弃)。

一个web资源(B)收到客户端A请求后,B会通知客户端A去访问另外一个web资源C,这个过程叫重定向

Response1

 1  package com.xing.servlet;
 2  3  import javax.servlet.ServletException;
 4  import javax.servlet.http.HttpServlet;
 5  import javax.servlet.http.HttpServletRequest;
 6  import javax.servlet.http.HttpServletResponse;
 7  import java.io.IOException;
 8  9  public class Response1 extends HttpServlet {
10 11      @Override
12      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13          System.out.println("曾到此一游 Response1 ");
14 15          req.setAttribute("key1", "value1");
16          //法一
17          // 设置响应状态码302,表示重定向
18          // resp.setStatus(302);
19          // 设置响应头,说明新的地址在哪里    参数形式 类似map   将Location设置为http://localhost:8080/07_servlet/response2
20          // resp.setHeader("Location", "http://localhost:8080/07_servlet/response2");
21          // resp.setHeader("Location", "https://www.baidu.com/");//可以重定向到工程外的资源
22 23          //法二
24          resp.sendRedirect("http://localhost:8080/07_servlet/response2");//resp.sendRedirect("/07_servlet/response2");
25      }
26  }

Response2

 1  package com.xing.servlet;
 2  3  import javax.servlet.ServletException;
 4  import javax.servlet.http.HttpServlet;
 5  import javax.servlet.http.HttpServletRequest;
 6  import javax.servlet.http.HttpServletResponse;
 7  import java.io.IOException;
 8  9  public class Response2 extends HttpServlet {
10 11      @Override
12      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13          System.out.println(req.getAttribute("key1"));
14          //新的业务逻辑
15          resp.getWriter().write("response2's result!");
16      }
17  }

web.xml

 1  <?xml version="1.0" encoding="UTF-8"?>
 2  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5           version="4.0">
 6  7      <servlet>
 8          <servlet-name>Response1</servlet-name>
 9          <servlet-class>com.xing.servlet.Response1</servlet-class>
10      </servlet>
11      <servlet-mapping>
12          <servlet-name>Response1</servlet-name>
13          <url-pattern>/response1</url-pattern>
14      </servlet-mapping>
15      <servlet>
16          <servlet-name>Response2</servlet-name>
17          <servlet-class>com.xing.servlet.Response2</servlet-class>
18      </servlet>
19      <servlet-mapping>
20          <servlet-name>Response2</servlet-name>
21          <url-pattern>/response2</url-pattern>
22      </servlet-mapping>
23 24  </web-app>

访问:http://localhost:8080/07_servlet/response1

请求的转发

什么是请求的转发(相当于呼叫转移)?

请求转发是指服务器收到请求后,从一个资源跳转到另一个资源的操作叫请求转发。

测试1:

Servlet1

 1  package com.xing.servlet;
 2  3  import javax.servlet.RequestDispatcher;
 4  import javax.servlet.ServletException;
 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  public class Servlet1 extends HttpServlet {
11      @Override
12      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13 14          // 1.获取请求的参数(办事的材料)查看
15          String username = req.getParameter("username");
16          System.out.println("在Servlet1(柜台1)中查看参数(材料):" + username);
17 18          // 2.给材料 盖一个章,并传递到Servlet2(柜台 2)去查看
19          req.setAttribute("key1","柜台1的章");
20 21          // 3.问路:Servlet2(柜台 2)怎么走
22 23          // 请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA代码的web目录       转发到哪里
24          RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
25 26          //4.走向Sevlet2(柜台 2)   进行转发
27          requestDispatcher.forward(req,resp);
28 29      }
30  }

Servlet2

 1  package com.xing.servlet;
 2  3  import javax.servlet.ServletException;
 4  import javax.servlet.http.HttpServlet;
 5  import javax.servlet.http.HttpServletRequest;
 6  import javax.servlet.http.HttpServletResponse;
 7  import java.io.IOException;
 8  9  public class Servlet2 extends HttpServlet {
10      @Override
11      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12          // 1.获取请求的参数(办事的材料)查看
13          String username = req.getParameter("username");
14          System.out.println("在Servlet2(柜台2)中查看参数(材料):" + username);
15 16          // 2.查看 柜台1 是否有盖章
17          Object key1 = req.getAttribute("key1");
18          System.out.println("柜台1是否有章:" + key1);
19 20          // 3.处理自己的业务
21          System.out.println("Servlet2 处理自己的业务 ");
22      }
23  }

web.xml

 1  <?xml version="1.0" encoding="UTF-8"?>
 2  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5           version="4.0">
 6  7      <servlet>
 8          <servlet-name>Servlet1</servlet-name>
 9          <servlet-class>com.xing.servlet.Servlet1</servlet-class>
10      </servlet>
11      <servlet-mapping>
12          <servlet-name>Servlet1</servlet-name>
13          <url-pattern>/servlet1</url-pattern>
14      </servlet-mapping>
15 16 17      <servlet>
18          <servlet-name>Servlet2</servlet-name>
19          <servlet-class>com.xing.servlet.Servlet2</servlet-class>
20      </servlet>
21      <servlet-mapping>
22          <servlet-name>Servlet2</servlet-name>
23          <url-pattern>/servlet2</url-pattern>
24      </servlet-mapping>
25 26  </web-app>

访问 localhost:8080/07_servlet/servlet1?username=xiaoming 传了参数,参数为xiaoming

测试2:

请求转发可以转发到WEB-INF目录下

 1 package com.xing.servlet;
 2  3  import javax.servlet.RequestDispatcher;
 4  import javax.servlet.ServletException;
 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  public class Servlet1 extends HttpServlet {
11      @Override
12      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13 14          String username = req.getParameter("username");
15          System.out.println("在Servlet1(柜台1)中查看参数(材料):" + username);
16 17          req.setAttribute("key1","柜台1的章");
18          RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/form.html");
19          //RequestDispatcher requestDispatcher = req.getRequestDispatcher("http://www.baidu.com"); //不可以 相当于访问 http://localhost:8080/07_servlet/http://www.baidu.com
20 21          //4.走向WEB-INF/form.html
22          requestDispatcher.forward(req,resp);
23 24      }
25  }

base 标签的作用

index.html

 1  <!DOCTYPE html>
 2  <html lang="zh_CN">
 3  <head>
 4      <meta charset="UTF-8">
 5      <title>首页</title>
 6  </head>
 7  <body>
 8      这是Web下的index.html <br/>
 9      <a href="a/b/c.html">a/b/c.html (用a标签直接跳到c.html)</a><br/>
10 11      <a href="http://localhost:8080/07_servlet/forwardC">请求转发:a/b/c.html(利用servlet程序的请求转发 跳到c.html)</a><br/>
12      <a href="/">斜杠</a>
13  </body>
14  </html>

c.html

 1  <!DOCTYPE html>
 2  <html lang="zh_CN">
 3  <head>
 4      <meta charset="UTF-8">
 5      <title>Title</title>
 6  </head>
 7  <body>
 8      这是a下的b下的c.html页面<br/>
 9      <a href="../../index.html">跳回首页</a><br/>
10  </body>
11  </html>

ForwardC

 1  package com.xing.servlet;
 2  3  import javax.servlet.ServletException;
 4  import javax.servlet.http.HttpServlet;
 5  import javax.servlet.http.HttpServletRequest;
 6  import javax.servlet.http.HttpServletResponse;
 7  import java.io.IOException;
 8  9  public class ForwardC extends HttpServlet {
10      @Override
11      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12          System.out.println("经过了ForwardC程序");
13          req.getRequestDispatcher("/a/b/c.html").forward(req, resp);
14      }
15  }

web.xml

 1  <?xml version="1.0" encoding="UTF-8"?>
 2  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5           version="4.0">
 6      
 7      <servlet>
 8          <servlet-name>ForwardC</servlet-name>
 9          <servlet-class>com.xing.servlet.ForwardC</servlet-class>
10      </servlet>
11      <servlet-mapping>
12          <servlet-name>ForwardC</servlet-name>
13          <url-pattern>/forwardC</url-pattern>
14      </servlet-mapping>
15 16  </web-app>

访问http://localhost:8080/07_servlet/index.html

测试1:点击 a/b/c.html (用a标签直接跳到c.html) 跳转到

点击跳回首页可以跳回首页

测试2:点击 请求转发:a/b/c.html(利用servlet程序的请求转发 跳到c.html) 跳转到

点击跳回首页不可以跳回首页

解决方法:base标签

c.html

 1  <!DOCTYPE html>
 2  <html lang="zh_CN">
 3  <head>
 4      <meta charset="UTF-8">
 5      <title>Title</title>
 6      <!--base标签设置页面相对路径工作时参照的地址
 7              href 属性就是参数的地址值
 8      -->
 9      <base href="http://localhost:8080/07_servlet/a/b/c.html">
10         <!-- 最后的c.html 可以不写  因为不是文件夹
11          <base href="http://localhost:8080/07_servlet/a/b/">
12          -->
13      
14  </head>
15  <body>
16      这是a下的b下的c.html页面<br/>
17      <!--会相对base标签进行跳转  这种相对路径都是基于base标签进行的(这个是基本base目录回退两级),有时候会出现问题,一般不用-->
18      <a href="../../index.html">跳回首页</a><br/>
19  </body>
20  </html>

 

 
posted @ 2022-05-23 09:01  暴躁C语言  阅读(39)  评论(0)    收藏  举报