response对象

response 代表服务器对客户端的响应.大部分时候,程序无须使用response来响应客户端请求.,因为还有一个更简单的响应对象 - out,它代表页面输出流,直接使用out生成响应更简单.
out是JspWriter的实例,jspWriter是writer的子类,writer是字符流,无法输出非字符内容.比如要在jsp页面中动态生成一幅位图,或者输出一个PDF文档,,使用out无法响应完成,此时必须使用response作为响应输出.

response使用的情况:

1.response响应生成非字符响应.
2.重定向
重定向是response的另外一个用处,与forward不同的是,重定向会丢失所有的请求参数和request范围的属性,因为重定向将生成第二次请求,与前一次请求不在同一个request范围内,所以发送一次请求的请求参数和request范围的属性全部消失.
HttpServletResponse提供了一个senRedirect(String path)方法,该方法用于重定向到path资源,即重新向path资源发送请求.
doRedirect.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%

response.sendRedirect("helloWorld.jsp");
%>
</body>
</html>


重定向后地址栏改变,参数和参数值已经丢失,request范围属性失效




转发forword 重定向(redirect)

forward的目标页面可以访问原请求 redirect的目标的页面不
的请求参数,依然是同一请求,所有的 能访问原请求的请求参数
请求参数,request范围属性全部存在 因为是第二次请求,参数,
request范围属性会消失

地址栏里请求的URL不会改变 地址栏改为重定向的目标URL
























### 处理 HTTP 错误的方式 #### 使用 `response.status` 进行条件判断 通过检查 `response.status` 的值,可以判断响应的状态码是否在正常范围内。一般来说,状态码在 200 - 299 之间表示请求成功。示例代码如下: ```javascript fetch('https://example.com/api/data') .then(response => { if (response.status >= 200 && response.status < 300) { return response.json(); } else { throw new Error(`HTTP error! status: ${response.status}`); } }) .then(data => { console.log(data); }) .catch(error => { console.error('Fetch error:', error); }); ``` #### 封装 `fetch` 请求函数 将 `fetch` 请求封装成一个函数,在函数内部统一处理错误,提高代码复用性。示例代码如下: ```javascript function customFetch(url) { return fetch(url) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); }); } customFetch('https://example.com/api/data') .then(data => { console.log(data); }) .catch(error => { console.error('Fetch error:', error); }); ``` #### 使用第三方库 使用 `axios` 库,它内部已经对 HTTP 错误进行了封装处理。示例代码如下: ```javascript import axios from 'axios'; axios.get('https://example.com/api/data') .then(response => { console.log(response.data); }) .catch(error => { if (error.response) { console.error('Server responded with an error:', error.response.status); } else if (error.request) { console.error('No response received:', error.request); } else { console.error('Error setting up the request:', error.message); } }); ``` ### 解决 response 对象无法打印的问题 #### 检查响应格式 如果响应不是 JSON 格式,直接调用 `response.json()` 会报错。可以先检查响应的 `Content-Type` 头信息。示例代码如下: ```javascript fetch('https://example.com/api/data') .then(response => { const contentType = response.headers.get('Content-Type'); if (contentType && contentType.includes('application/json')) { return response.json(); } else { return response.text(); } }) .then(data => { console.log(data); }) .catch(error => { console.error('Fetch error:', error); }); ``` #### 处理异步问题 `fetch` 是异步操作,确保在响应返回后再打印。如果在响应还未返回时就尝试打印,可能会得到未定义或不完整的结果。 #### 检查网络请求是否成功 如果网络请求失败,`response` 对象可能不包含预期的数据。可以使用前面提到的错误处理方法,确保请求成功后再处理响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值