response响应信息(重定向、定时刷新、设置响应类型、打开资源方式)

Response

响应信息
通过状态码:判断响应是否成功

Status:

200 -----成功响应
500 -----服务器出现了异常(servlet(和业务相关的数据)由 tomcat)
404 -----请求 url 路径有问题
302 -----进一步请求

响应头:
loaction:一个地址 + 302:重定向
简写方式:response。sendRedirect (".项目名称/hello.html")

重定向的特点:

  1. 发送了两次请求(产生两个 request 对象)
  2. request:本身域对象(共享数据)

重定向 / 请求转发(servletContext对象)跳转页面
refersh:定时刷新或者经过 n 秒跳转页面
响应头(已下载方式打开)
文件下载:使用第三方 jar 包 commons-io

1. 重定向的原理

重定向——>过程中。有几个request对象?
请求转发——>过程中,有几个request对象?
response响应信息(重定向、定时刷新、设置响应类型、打开资源方式)_第1张图片

package com.github.urlpattern;

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 MappingDemo1 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("mappingDemo1.doGet()方法调用了...");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

在这里插入图片描述

package com.github.urlpattern;

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 MappingDemo2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("mappingDemo2.doGet()方法调用了...");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

response响应信息(重定向、定时刷新、设置响应类型、打开资源方式)_第2张图片

响应头:loaction + status(302) -------进一步请求
重定向:原理:location + 302

adv.html:


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>广告页面title>
head>
<body>
广告页面<br>
<a href="/RequestDemo5">跳转到资源地址a>
body>
html>
package com.github.response;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求重定向
 * location请求头+302状态码
 */
public class ResponseDemo1 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 重定向的原理:
        // 设置状态302
        // 1.
//        response.setStatus(302); // 进一步请求服务器
//        // 设置location响应头
//        response.setHeader("location","/adv.html");

        // 2.
        // 简便的方法
        response.sendRedirect("/adv.html");

        // 3.
        // 地址栏不会发生变法,服务器行为
        //request.getRequestDispatcher("/adv.html").forward(request,response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

地址栏输入:
在这里插入图片描述
回车之后:
在这里插入图片描述

2. refersh:响应头 定时刷新,每隔n秒跳转资源

package com.github.response;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * refersh:响应头  定时刷新,每隔n秒跳转资源
 */
public class ResponseDemo2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        // 1.
        // 设置响应头:每经过多少秒实现页面刷新
        //response.setHeader("refersh","2");

        // 2.
        response.getWriter().write("注册成功!3秒后跳转首页");
        response.setHeader("refresh","3;/adv.html");

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

response响应信息(重定向、定时刷新、设置响应类型、打开资源方式)_第3张图片
3秒之后刷新页面,重定向到一个新的页面:adv.html:
在这里插入图片描述

3. content-type:响应头:设置当前响应的类型:html/xml/图片资源

package com.github.response;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * content-type:响应头:设置当前响应的类型:html/xml/图片资源
 */
public class ResponseDemo3 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        //response.setHeader("content-type","text/html;charset=utf-8");
        // 简写方式
        //response.setContentType("text/html;charset=utf-8");

        // 当前向浏览器输出的格式支持html格式
        //response.setContentType("text/html,charset=utf-8");
        //response.getWriter().write("this is a html页面...");

        // 当作自定义标签进行输出
        response.setContentType("text/xml");
        response.getWriter().write("this is a xml...");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

response响应信息(重定向、定时刷新、设置响应类型、打开资源方式)_第4张图片

4. Content-Disposition: attachment; filename=aaa.zip ---- 以下载方式打开资源

package com.github.response;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Content-Disposition: attachment; filename=aaa.zip
 * ---- 以下载方式打开资源
 */
public class ResponseDemo4 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.
        // 下载文件
        // 创建一个字节输入流对象
//        InputStream in = new FileInputStream("C:\\Users\\28187\\Pictures\\Saved Pictures\\壁纸\\574.jpg");
//        // 获取一个字节输出流对象
//        OutputStream out = response.getOutputStream();
//        // 边读边写
//        byte[] buff = new byte[1024];
//        int len = 0;
//        while ((len=in.read(buff)) != -1) {
//            out.write(buff,0,len);
//        }
//        // 释放资源
//        in.close();
//        out.close();

        // 2.
        // 创建File对象
        File file = new File("C:\\Users\\28187\\Pictures\\Saved Pictures\\壁纸\\574.jpg");

        // Content-Disposition:响应头
        response.setHeader("Content-Disposition","attachment;filename="+file.getName());
        // 下载文件
        // 创建一个字节输入流对象
        InputStream in = new FileInputStream("C:\\Users\\28187\\Pictures\\Saved Pictures\\壁纸\\574.jpg");
        // 获取一个字节输出流对象
        OutputStream out = response.getOutputStream();
        // 边读边写
        byte[] buff = new byte[1024];
        int len = 0;
        while ((len=in.read(buff)) != -1) {
            out.write(buff,0,len);
        }
        // 释放资源
        in.close();
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

直接开始下载内容:
response响应信息(重定向、定时刷新、设置响应类型、打开资源方式)_第5张图片

你可能感兴趣的:(Web前端)