• 分析:重定向和请求转发


    分析:重定向和请求转发

    重定向

      HttpServletResponse对象的sendRedirect(java.lang.String location)方法称作重定向

      如果location地址前面加上“/”,则表示相对于Servlet容器的根来请求,比如http://localhost:8080;如果location地址前面没有加上“/”,则表示相对于当前请求的URI来寻找地址。

    请求转发

      RequestDispatcher的:forward(ServletRequest request, ServletResponse response)方法叫做请求转发。

      

    实验例子1:重定向和请求转发似乎都是造成页面跳转

      第一个页面first.jsp:

    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'first.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <form action="Second">
        <input type="text" name="username">
        <input type="submit" value="submit">
        </form>
      </body>
    </html>
    复制代码

      第二个页面是Servlet:

      用请求转发:

    复制代码
    package com.shengqishiwind.servlet;
    
    import java.io.IOException;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Second extends HttpServlet
    {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            process(request, response);
    
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            process(request, response);
    
        }
    
        private void process(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException
        {
            // 请求转发
            RequestDispatcher rd = request.getRequestDispatcher("third.jsp");
            rd.forward(request, response);
        }
    
    }
    复制代码

      用重定向,则把处理方法改为:

    复制代码
        private void process(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException
        {
            // 重定向
            response.sendRedirect("third.jsp");
    
        }
    复制代码

      第三个页面是third.jsp

    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'third.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        This is my Third page. <br>     
      </body>
    </html>
    复制代码

      不管用请求转发还是重定向的方法,第一个页面点击提交后,都能顺利转到第三个页面:

      但是其实实际进行的操作还是很不同的,看下面的例子。

    实验例子2:如果要在第三个页面中取得第一个页面输入的用户名

      请求转发的实现比较简单,第二个页面的Servlet代码不用添加任何东西,可以直接从第三个页面getParameter,即把third.jsp中的body改为:

      <body>
        This is my Third page. <br> 
           用户名:<%=request.getParameter("username") %>    
      </body>

      则在第一个页面输入shengqishiwind,提交后,第三个页面显示:

      重定向应该怎么实现获取第一个页面提交的用户名呢?

      第三个页面采用如上的代码(通过getParameter获取参数),把第二个页面中改为重定向的跳转方式,重新来一次,可以看到显示:

      再尝试第二个页面这样(通过setAttribute存储值):

    复制代码
        private void process(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException
        {
            String username = request.getParameter("username");
            // 重定向
            request.setAttribute("username", username);
            response.sendRedirect("third.jsp");
    
        }
    复制代码

      然后第三个页面这样获取:

         用户名:<%=request.getAttribute("username") %>    

      仍然是不行,得到的仍然是null值。

    重定向和请求转发的区别

    请求转发:

      RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法。

      请求转发,整个过程处于同一个请求当中。 

      不管经历了多少组件,都可以从request中直接取得想要的值。

      使用请求转发时,到结果页面的网址是:http://localhost:8080/HelloWeb/Second?username=wind

    重定向: 

      sendRedirect()是HttpServletResponse对象的方法,即响应对象的方法。

      既然调用了响应对象的方法,那就表明整个请求过程已经结束了,服务器开始向客户端返回执行的结果。

      sendRedirect调用后会向客户端返回一个响应,这个响应告诉客户端要转向的页面,紧接着客户端又会发送一个新的请求,转向这个目标页面。

      也即是说,重定向的过程中实际上客户端会向服务器发送两个请求。

      使用重定向时,结果页面的网址是:http://localhost:8080/HelloWeb/third.jsp

      说明客户端已经知道了结果页面的地址,是重新发送的全新的请求。

      重定向方式在Firebug中的图:

      总结记忆请求转发只有一个请求,所以勇往直前,调用的方法叫forward;而重定向需要客户端重新发送请求,调用的是sendRedirect,名字里有个Re,表示重复。

    参考资料:

      圣思园张龙老师视频教程。

      Java Document API with examples:

      http://www.javadocexamples.com/

     

  • 相关阅读:
    可爱的中国电信 请问我们的电脑还属于我们自己吗?
    了解客户的需求,写出的代码或许才是最优秀的............
    DELPHI DATASNAP 入门操作(3)简单的主从表的简单更新【含简单事务处理】
    用数组公式获取字符在字符串中最后出现的位置
    在ehlib的DBGridEh控件中使用过滤功能(可以不用 MemTableEh 控件 适用ehlib 5.2 ehlib 5.3)
    格式化json返回的时间
    ExtJs中使用Ajax赋值给全局变量异常解决方案
    java compiler level does not match the version of the installed java project facet (转)
    收集的资料(六)ASP.NET编程中的十大技巧
    收集的资料共享出来(五)Asp.Net 权限解决办法
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4840363.html
Copyright © 2020-2023  润新知