响应消息resp

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/respdemo1")
public class TestRespOServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        /*
        服务器发给浏览器的数据

        * 响应头:
        HTTP/1.1 200
        Content-Type: text/html;charset=UTF-8
        Content-Length: 90
        Date: Sat, 14 Aug 2021 14:19:17 GMT
        Keep-Alive: timeout=20
        Connection: keep-alive

        * 响应体:
        <html>
          <head>
            <title>$Title$</title>
          </head>
          <body>
          $END$
          </body>
        </html>

        
        */
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------");

        /*
        * setStatus(int sc);    设置状态码
        *   状态码有五种:
        *       1xx:
        *       2xx:
        *       3xx: 302重定向/304访问缓存
        *       4xx:
        *       5xx:
        * 以上就是设置响应行的方法
        * */

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------");


        /*
        * setHeader(String name, String value);
        * 这个方法是用来设置响应头的消息的
        *
        * 响应消息的常用响应头:
        *   location: 告诉浏览器, 需要重定向的地址
        *   content-type: 服务器告诉浏览器本次响应消息的响应体的数据格式以及编码格式
        *   content-disposition: 服务器告诉客户端以什么格式打开响应体数据
        *       in-line: 默认值, 在当前页面
        *       attachment; filename=xxx; 以附件的形式打开.
        *
        *
        * 以上就是设置响应头的方法
        * */

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------");

        /*
        * 与请求消息类似
        * 响应消息的响应体也是需要特殊的容器的
        * 不是简单的字符串
        * 而是字符流或者字节流
        * 只不过,这里不是Reader咯
        *       字符流: PrintWriter getWriter();
        *       字节流: ServletOutputStream getOutputStream();
        * */
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------");

        System.out.println("这是demo1");
        //resp.setStatus(302);
        //resp.setHeader("location","/814_resp_war_exploded/respdemo2");
        /*
        * 注意,resp的重定向和req的请求转发不一样
        * resp的重定向是浏览器再访问一次
        * 也就是有两次请求
        * 浏览器的地址栏信息就会改成新的信息
        * 重定向还可以访问其他站点的资源,而不仅仅是当前的站点
        *
        * 这里有个简单的方法
        * sendRedirect(String uri);
        *
        *
        *
        * 以上就是重定向的知识*/

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------");

        //获取流之前, 要先将流的默认编码iso改成gbk
        //resp.setCharacterEncoding("utf-8");
        //PrintWriter writer = resp.getWriter();
        //writer.write("<h1>你好, resp</h1>");
        /*果不其然, 会乱码*/
        /*就不会乱码了*/
        /*可是, 我们事先并不知浏览器的编码是GbK的怎么办呢
        * 是不是要要求浏览器按照我的响应信息体去解码*/
        //resp.setHeader("content-type","text/html;charset=utf-8");
        //这样一来,我们一切都拿捏了
        //可是每次都要写这两行代码resp.setCharacterEncoding("utf-8"); resp.setHeader("content-type","text/html;charset=utf-8");我觉得麻烦
        //简单做法就是
        //resp.setContentType("text/html;charset=utf-8");
        //将这行代码写在最前面,获取流之前.
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------");

        /*字符流只能往页面输送字符数据
        * 还是有一定的局限性的
        * */
        resp.setContentType("text/html;charset=utf-8");
        ServletOutputStream outputStream = resp.getOutputStream();
        /*这个字节流就可以输送任意的数据了*/
        outputStream.write("你好, resp1".getBytes("utf-8"));

        /*
        * 有代码可知
        * 对于字节流
        * resp.setContentType("text/html;charset=utf-8");
        * 这行代码就不能同时设置流的编码和通知浏览器解码的编码了
        * 只是起到通知的作用
        * 真正起到设置流的编码格式的地方是输出时指定的数据类型时传入的参数
        * outputStream.write("你好, resp1".getBytes("utf-8"));*/


    }
}

Guess you like

Origin blog.csdn.net/weixin_45032905/article/details/121855690