请求转发与重定向

2019-05-21  本文已影响0人  聪明的小一休

请求转发

指将请求再转发到其他地址,转发过程中使用的是同一个request请求,转发后浏览器地址栏内容不变。
请求转发使用RequestDispatcher接口中的forword()方法实现,该方法可以把请求转发给另外一个资源,并让该资源对此请求进行响应。
实现方式
request.getRequestDispatcher("HelloServlet2").forward(request, response);

package com.neusoft.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class HelloServlet1
 */
@WebServlet("/HelloServlet1")
public class HelloServlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet1() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        System.out.println("*******"+request.getServletContext().getInitParameter("url"));//*******null
        String username=request.getParameter("username");
        System.out.println(response.getWriter().append("HelloServlet1"+username));//org.apache.catalina.connector.CoyoteWriter@57e5b570
        System.out.println(request.getServletContext().getContextPath()+"HelloServlet2");///zhihu20190518HelloServlet2
        //  System.out.println("username:"+username);
        //请求转发 
        request.getRequestDispatcher("HelloServlet2").forward(request, response);
        //(响应)重定向
        //  response.sendRedirect("HelloServlet2");
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
package com.neusoft.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class HelloServlet2
 */
@WebServlet("/HelloServlet2")
public class HelloServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
//      response.getWriter().append("Served at: ").append(request.getContextPath());
        String username=request.getParameter("username");
        System.out.println(response.getWriter().append("HelloServlet2"+username));//org.apache.catalina.connector.CoyoteWriter@57e5b570
//      System.out.println("username:"+username);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
image.png
image.png

重定向

指由原请求地址重新定位到某个地址,原有的request请求失效,客户端看到的是新的request请求返回的响应结果,客户端浏览器地址栏变为新请求地址。
实现方式:
response.sendRedirect("HelloServlet2");

package com.neusoft.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class HelloServlet1
 */
@WebServlet("/HelloServlet1")
public class HelloServlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet1() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
//      response.getWriter().append("Served at: ").append(request.getContextPath());        System.out.println("*******"+request.getServletContext().getInitParameter("url"));//*******null
        String username=request.getParameter("username");
    System.out.println(response.getWriter().append("HelloServlet1"+username));//org.apache.catalina.connector.CoyoteWriter@28a20346 System.out.println(request.getServletContext().getContextPath()+"HelloServlet2");///zhihu20190518HelloServlet2
    //  System.out.println("username:"+username);
    //请求转发 
//  request.getRequestDispatcher("HelloServlet2").forward(request, response);
    //(响应)重定向
    response.sendRedirect("HelloServlet2");
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
package com.neusoft.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class HelloServlet2
 */
@WebServlet("/HelloServlet2")
public class HelloServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
//      response.getWriter().append("Served at: ").append(request.getContextPath());
        String username=request.getParameter("username");       System.out.println(response.getWriter().append("HelloServlet2"+username));//org.apache.catalina.connector.CoyoteWriter@28a20346
//      System.out.println("username:"+username);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
image.png
image.png

请求转发与重定向区别

1.请求转发只能将请求转发给同一个Web应用中的组件;而重定向不仅可以重定向到当前应用程序中的其他资源,还可以重定向到同一个站点上的其他应用程序中的资源,或者重定向到其他站点的资源。
2.重定向的访问过程结束后,浏览器地址栏中显示的URL会发生改变,由初始的URL地址变为重定向的目标URL;而请求转发过程结束后,浏览器地址栏保持初始的URL地址不变;
3.请求转发调用者与被调用者之间共享相同的request对象和response对象,他们属于同一个访问请求和响应过程;而重定向调用者与被调用者使用各自的request对象和response对象,他们属于两个独立的访问请求和响应过程。
4.不代表方法的结束,下面的代码还会继续执行,注意在转发或重定向的语句后面不要在编写代码。

上一篇下一篇

猜你喜欢

热点阅读