首页 > 其他分享 >Response-输出字符数据、输出字节数据

Response-输出字符数据、输出字节数据

时间:2023-02-09 10:33:38浏览次数:42  
标签:输出 Response HttpServletResponse javax import servlet response 字节

Response-输出字符数据
  服务器输出字符数据到浏览器

    步骤:

      1.获取字符输出流

      2.输出数据

    注意:

      乱码问题

        1.PrintWriter pw = response.getWriter();获取的流的默认编码是ISO-8859-1

        2.设置该流的默认编码

        3.告诉浏览器响应体使用的编码

          简单的形式,设置编码,是在获取流之前设置

          response.setContentType("text/html;charset=utf-8");

package com.example.day_14_response;

import javax.servlet.ServletException;
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("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取流对象之前,设置流的默认编码:ISO-8859-1 设置为:GBK
       // response.setCharacterEncoding("utf-8");

        //告诉浏览器,服务器发送的消息体数据的编码。建议浏览器使用该编码解码
        //response.setHeader("content-type","text/html;charset=utf-8");

        //简单的形式,设置编码
        response.setContentType("text/html;charset=utf-8");

        //1.获取字符输出流
        PrintWriter pw = response.getWriter();
        //2.输出数据
        //pw.write("<h1>hello response</h1>");
        pw.write("你好啊啊啊 response");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

Response-输出字节数据

  服务器输出字节数据到浏览器

    步骤:

      1.获取字节输出流

      2.输出数据

package com.example.day_14_response;

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;

@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        //1.获取字节输出流
        ServletOutputStream sos = response.getOutputStream();
        //2.输出数据
        sos.write("你好".getBytes("utf-8"));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

标签:输出,Response,HttpServletResponse,javax,import,servlet,response,字节
From: https://www.cnblogs.com/wsfj/p/17104351.html

相关文章