javaweb中请求转发和请求重定向详解(有案例,有路径问题)

目录

请求转发

请求重定向

请求转发和请求重定向路径问题


  • 请求转发

请求转发:客户端发送一次请求,经过服务器处理后,转发个前端网页或者另一个Servlet,它的本质是同一次请求,会共享请求域中的数据。

特点:

  1. 浏览器的地址栏没有发生变化
  2. 不管经过多少次转发,都是同一个请求
  3. 他们共享请求域中的数据
  4. 可以转发到WEB-INF目录下
  5. 不可以直接访问外部文件

在Tomcat 服务器的Servlet中默认地址:http://ip:8080/工程名     

路径中以斜杠开头代表绝对路径,否则是相对路径

路径中第一个斜杠代表:http://ip:8080/工程名

   

案例:

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/web02/servlet1" method="get">
    账号: <input type="text" name="username"><br>
    密码: <input type="password" name="password"><br>
    兴趣爱好<input type="radio" name="hobby" value="美女">美女<input type="radio" name="hobby" value="帅哥">帅哥<br>
    <input type="submit">
</form>
</body>
</html>

servlet:

public class servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //获得请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+"\t"+password);

        //为请求1盖章(设置全域变量)
        req.setAttribute("key","complete");

        //请求转发:无法完成外部跳转,可以访问内部资源,
        //req.getRequestDispatcher("/servlet2").forward(req,resp);
       req.getRequestDispatcher("http://www.baidu.com").forward(req,resp);
        //请求重定向。可以实现外部跳转。
       //resp.sendRedirect("http://www.baidu.com");  


    }
}

请求转发访问外部资源报错:

HTTP Status 404 - /web02/http://www.baidu.com

type Status report

message /web02/http://www.baidu.com

description The requested resource is not available.


Apache Tomcat/8.0.50

  • 请求重定向

请求重定向:客户端给服务器发请求,然后服务器返回给客户端一个新的地址,让其去请求新的地址。本质上是二个不同的请求。

特点:

  1. 浏览器地址栏会发生变化
  2. 本质上是二次不同的请求
  3. 不共享请求域中的数据
  4. 不能访问WEB-INF下的资源
  5. 可以访问工程外的资源

案例:

前端代码一样用前面的

public class servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
         //获得请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+"\t"+password);
        //为请求1盖章(设置全域变量)
        req.setAttribute("key","complete");

        //跳转到servlet2
//        req.getRequestDispatcher("WEB-INF/表格.jsp").forward(req,resp);
//          req.getRequestDispatcher("http://www.baidu.com").forward(req,resp);
 //         resp.sendRedirect("WEB-INF/table.jsp");
          resp.sendRedirect("http://www.baidu.com");


    }
}

访问WEB-INF报错,访问百度网址可以成功。

  • 请求转发和请求重定向路径问题

  • 绝对路径  :以/开头

请求重定向的 / 表示:http://服务器ip:端口/
response.sendRedirect("/servlet2");
地址为:http://localhost:8080/servlet2


请求转发的 / 表示:http://服务器ip:端口/项目名
request.getRequestDispatcher("/servlet2").forward(request, response);
地址为:http://localhost:8080/项目名/servlet2

相对路径:

请求重定向:response.sendRedirect(“servlet2”)

生成相对路径:http://localhost:8080/项目名/servlet2

请求转发:相对路径情况下生成完整URL与重定向相同

猜你喜欢

转载自blog.csdn.net/weixin_45533131/article/details/126827632
今日推荐