美文网首页
请求转发与重定向

请求转发与重定向

作者: 聪明的小一休 | 来源:发表于2019-05-21 08:04 被阅读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.不代表方法的结束,下面的代码还会继续执行,注意在转发或重定向的语句后面不要在编写代码。

相关文章

  • 重定向与转发

    请求重定向与请求转发的区别: 1.写法: 转发: request.getRequestsDispatcher.fo...

  • spring mvc中的重定向和转发

    <1> 不带参数的重定向 <2> 带参数的重定向 2、请求转发:// 转发到toList请求

  • Java请求转发和重定向的区别

    原作者:向右奔跑原博客链接:Java请求转发和重定向的区别 一、请求转发和重定向 1)请求转发: request....

  • 请求转发和重定向

    重定向 请求转发

  • JSP 内置对象(上)

    本文包括:out(常用)request(重点)response(重点)get与post(难点)请求重定向与请求转发...

  • JavaWeb:Cannot forward after res

    重定向了请求后抛出异常 解决方法:在每个转发或者重定向请求之后加上return语句(最后一个请求转发或者重定向不需...

  • 转发与重定向的区别

    转发和重定向的区别1、转发发生在服务器、重定向发生在客户端2、转发是一次请求、重定向是两次请求3、转发地址不发生改...

  • 重定向,请求转发,get,post

    重定向与请求转发 重定向意味着客户端接到服务器通知后,重新向服务器端发送请求,获得了一个新的页面. 请求转发没有获...

  • 请求重定向与请求转发

    本质区别:请求的转发只发出了一次请求,而重定向则发出了两次请求. 请求的转发:地址栏是初次发出请求的地址.请求的重...

  • Javaweb请求转发及重定向实现详解

    请求转发:在同一个controller将请求转发到另一个请求映射,请求地址不会发生改变 重定向:将请求重定向到不同...

网友评论

      本文标题:请求转发与重定向

      本文链接:https://www.haomeiwen.com/subject/rpgrzqtx.html