HttpServletResponse重定向

1.HTTP协议规定了一种重定向的机制,重定向的运作流程如下

  1. 用户在浏览器输入特定的URL,请求访问服务端的某个组件。
  2. 服务端的组件返回一个状态码为302的响应结果。该响应结果的含义为:让浏览器在请求访问另一个Web组件。在响应结果中 提供了另一个组件的URL。
  3. 当浏览器端接收到这种响应结果后,再立即自动请求访问另一个Web组件
  4. 浏览器接收到来自另一个Web组件的响应结果

2. 在JAVA Servlet API中 用于重定向的HttpServletResponse 接口

sendRedirect方法:

void sendRedirect(java.lang.String location)
throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code to SC_FOUND 302 (Found). This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Parameters:
location - the redirect location URL
  1. 注意是 HttpServletResponse,ServletResponse中是没有sendRedirect方法的。因为重定向机制是HTTP协议规定的。
  2. API中蓝色字体,表示response.sendRedirect(String location),如果参数location 以"/"开头 表示相对URL 。如果以"Http://"开头 表示绝对路径
  3. API字体中的金色字体,表示 如果在源组件进行重定向之前,已经提交了响应结果(调用ServletResponse相关的close()方法),那么sendRedirect方法会抛出IllegalStateException异常
  4. API中的紫色字体,表示 Servlet源组件生成的响应结果 不会被发送到客户端,sendRedirect一律返回302 浏览器接收到302状态码的时候 会立刻请求目标组件的URL。客户端接到的响应结果是目标Web组件的响应结果
  5.  源组件和目标组件不共享一个ServletRequest对象,因此不共享请求范围内的共享数据(显而易见 是浏览器重新像服务器发送了一个请求)

RequestDispatcher转发

转发是通过RequestDispatcher实现的

RequestDispatcher:请求分发器(故名思议: 同一个请求 分发不同的Servlet)

首先看实现转发的:forward方法

void forward(ServletRequest request,
ServletResponse response)
throws ServletException,
java.io.IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

This method sets the dispatcher type of the given request to DispatcherType.FORWARD.

Parameters:
request - a ServletRequest object that represents the request the client makes of the servlet
response - a ServletResponse object that represents the response the servlet returns to the client 
  1. Servlet(源组件) 先对客户请求做一些预处理的操作,然后把请求转发给其他Web组件(目标组件)来完成包括生成相应结果在内的后序操作
  2. 转发的时 源组件和目标组件共享一个ServletRequest对象和ServletResponse对象(不同于重定向,转发的时候浏览器并没有重新像服务器发送一个请求)

这是转发和重定向最大的区别

补充:RequestDispatcher除了有forward方法外 还有include()方法 include()和forward()的共同点是 源组件和目标组件共享一个ServletRequest对象

void include(ServletRequest request,
ServletResponse response)
throws ServletException,
java.io.IOException
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

This method sets the dispatcher type of the given request to DispatcherType.INCLUDE.

Parameters:
request - a ServletRequest object that contains the client's request
response - a ServletResponse object that contains the servlet's response

HttpServlet的转发和重定向的更多相关文章

  1. Servlet中转发和重定向的区别

    Servlet中页面的跳转有两种方式:转发和重定向. 1.转发和重定向的区别 ①转发是服务器行为,重定向是客户端行为. ②转发是浏览器发生了一次请求,重定向至少是两次请求. ③转发地址栏中的url不会 ...

  2. JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)

    1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...

  3. jsp:和属性相关的方法,请求的转发,重定向

    jsp中与属性相关的方法: 方法: void setAttribute(String name, Object o): 设置属性 Object getAttribute(String name):获取 ...

  4. 定向转发和重定向实现 <select >下拉表单数据传送

    定向转发的特点:   (1). 实行转发时浏览器上的网址不变  (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~)    (2). 实行转发时 :   只有一次请求.  不信,看这下面的 ...

  5. javaWEB总结(14):请求的转发和重定向

    通俗理解请求转发与重定向的流程 通俗的来讲:我们可以把请求转发和重定向当成是一种借钱的情景模式. (1)请求的转发:A向B借钱,B自己没有钱,但是向C借到了钱,并且把钱借给了A.A只向B请求了一次. ...

  6. HttpServletRequest 接口、HttpServletResponse 接口、请求转发与重定向

    上篇文章我们讲了servlet的基本原理,这章将讲一下剩余的部分. HttpServletRequest 接口 该接口是 ServletRequest 接口的子接口,封装了 HTTP 请求的相关信息, ...

  7. HttpRequest,HttpResponse,乱码,转发和重定向

    HttpServletRequest简介 Web服务器收到客户端的http请求,会针对每一次请求,创建一个用于代表请求的HttpServletRequest类型的request对象,并将"H ...

  8. Servlet(四):转发与重定向、路径问题

    在上次的小案例中用到了转发的技术,今天来仔细聊聊转发和重定向的问题,以及一些小知识的汇总. 一.转发 1.转发的概念 转发主要是将浏览器的请求交给另外一个servlet或jsp来处理,借助reques ...

  9. 04_web基础(六)之请求转发与重定向

    1.交互方式 Web组件之间跳转: 从AServlet 跳转到 BServlet. 三种类型: 1:请求转发(forward) 2:URL重定向(redirect) 3:请求包含(include) 3 ...

随机推荐

  1. sonar之阿里巴巴java规则(p3c)

    今天为了打包生成sonar-p3c-pdm插件,折腾了半天.sonar版本v6.7.6,p3c源码地址https://gitee.com/jasonlong10/sonar-p3c-pmd-plugi ...

  2. java进阶系列之装饰器模式

    1.介绍 装饰器模式顾名思义就是装饰某个对象的,是一种结构型模式.装饰器模式允许向一个现有对象添加新的功能,同时不改变其结构,用户可以随意的扩展原有的对象.它是作为现有的类的一个包装.装饰器模式一方面 ...

  3. 进军的socket

    在学socket有时候我们会遇到这种问题: 解决方法一: 在服务端中加入:severTCP.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ...

  4. leetcode8:字符串转整数 (atoi)

    实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值 ...

  5. todolist---插入和删除----vue

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. MAC使用超级终端

    其实很简单. 先用ls看看/dev/tty.*哪个是具体的串口,我这里使用的edison的板子,所以插到macos上后可以看到的设备节点为: 只需要使用screen 命令即可,具体的命令格式如下: s ...

  7. 联想功能 Jquery autocomplete.js输入框联想补全功能

    转载地址:https://www.cnblogs.com/jinzhiming/p/6768402.html

  8. 【Linux】vim的使用

    使用vi和vim的原因:linux很多软件默认调用vi进行编辑,因此有必要熟悉它的使用规则 vi: 打开文件: vi 文件名 [一般模式]打开文件时进入一般模式,这个模式下的操作: 上下左右移动光标 ...

  9. java35

    1.变量的访问:就近原则 2.this.name 本类的name 3.枚举:一个事物的固定状态 修饰符 enum 枚举名称{ } 4.枚举相当于一个特殊的类,默认继承了Enum 5.枚举不能直接创建对 ...

  10. 【NIFI】 Apache NiFI 使用技巧

    本章介绍NIFI组件的使用. 主要有:Nginx反向代理NIFI,配置SSLContextService Nginx反向代理NIFI 使用nginx反向代理NIFI配置如下 upstream nifi ...