1.状态行
protocol: http协议版本
status code: 状态码
status message: 状态描述
2.响应头
服务器定义内容, 把内容通过http响应返回到客户端!
3.响应内容
响应是从服务器向客户端(浏览器)写入的!
响应总共有两个类型
1.响应文本 字符串
把字符串传输到浏览器, 然后由浏览器负责解析!
//响应文本
response.getWriter();
2.响应流 文件
文件下载
//响应流
response.getOutputStream();
package com.javakc.web1.user;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
//response常用方法
public class UserServlet7 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从服务器端向客户端返回响应头信息
response.setHeader("username", "admin");
//响应文本
//向客户端返回html
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write("Hello world
");
writer.flush();
writer.close();
//向客户端返回json
response.setContentType("application/json;charset=UTF-8");
PrintWriter writer1 = response.getWriter();
String json = "{\"name\": \"admin\", \"age\": 21}";
writer1.write(json);
writer1.flush();
writer1.close();
//响应流
//1.指定响应类型 图片
response.setContentType("image/jpeg");
//2.获取文件输入流
String path = "C:\\Users\\a\\Pictures\\谭晓聪\\txc.jpg";
//3.获取响应流
OutputStream out = response.getOutputStream();
out.write(Files.readAllBytes(Path.of(path)));
out.flush();
out.close();
//1.指定响应类型 图片
response.setContentType("image/jpeg");
//2.获取文件输入流
String path = "C:\\Users\\a\\Pictures\\谭晓聪\\txc.jpg";
File imageFile = new File(path);
InputStream input = new FileInputStream(imageFile);
//2.获取响应流
OutputStream out = response.getOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = input.read(b)) > 0) {
out.write(b, 0, len);
}
out.flush();
out.close();
//文件下载
//1.指定响应类型 图片
response.setContentType("image/jpeg");
//2.获取文件输入流
String path = "C:\\Users\\a\\Pictures\\谭晓聪\\txc.jpg";
//3.获取响应流
//重置响应
response.reset();
//通知浏览器 响应类型
response.setHeader("Content-Disposition", "attachment;filename=1.jpg");
OutputStream out = response.getOutputStream();
out.write(Files.readAllBytes(Path.of(path)));
out.flush();
out.close();
//1.指定响应类型 图片
response.setContentType("image/jpeg");
//2.获取文件输入流
String path = "C:\\Users\\a\\Pictures\\谭晓聪\\txc.jpg";
File imageFile = new File(path);
InputStream input = new FileInputStream(imageFile);
//3.获取响应流
//重置响应
response.reset();
//通知浏览器 响应类型
response.setHeader("Content-Disposition", "attachment;filename=1.jpg");
OutputStream out = response.getOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = input.read(b)) > 0) {
out.write(b, 0, len);
}
out.flush();
out.close();
}
}
package com.javakc.web1.user;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet8 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//分发器跳转页面
request.getRequestDispatcher("/success.html").forward(request, response);
//重定向跳转
response.sendRedirect(request.getContextPath() + "/success1.html");
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>successtitle>
head>
<body>
success
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success1title>
head>
<body>
success1
body>
html>
分发器 | 重定向 | |
---|---|---|
是否携在参数 | 携在参数 | 不携在参数 |
请求次数 | 一次请求 | 两次请求 |
跨域 | 不支持 | 支持 |
地址栏 | 不会发生改变 | 会发生改变,新地址是重定向后的地址 |
web.xml
<error-page>
<error-code>404error-code>
<location>/error/404.htmllocation>
error-page>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404title>
head>
<body>
很遗憾,页面找不到
body>
html>