JavaWeb-Response的使用

系列文章目录

JavaWeb中Response的使用



前言

Tomcat 控制台日志中文乱码问题解决方案
参考博文

在这里插入图片描述


一、Response设置响应数据功能介绍

在这里插入图片描述

二、Response完成重定向

在这里插入图片描述

package org.example.web.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;

@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

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

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


        //重定向
       /* //1.设置响应状态码302
        response.setStatus(302);

        //2.设置响应头Location
        response.setHeader("Location","/request_demo/resp2");*/

        //简化方式完成重定向
        response.sendRedirect("/request_demo/resp2");
    }
}

package org.example.web.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;

@WebServlet("/resp2")
public class ResponseDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

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

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


    }
}

三、Response响应字符数据

在这里插入图片描述

package org.example.web.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("/resp3")
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

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

        //设置流的编码并使用HTML解析
        response.setContentType("text/html;charset=utf-8");

        //1.获取字符输出流
        PrintWriter writer = response.getWriter();
        writer.write("aaa");
        //2.告诉浏览器,使用HTML格式解析数据
        //response.setHeader("content-type","text/html");

        writer.write("<h1>标题1</h1>");
    }
}

四、Response响应字节数据

在这里插入图片描述

package org.example.web.response;



import org.apache.commons.io.IOUtils;

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.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

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

        //1.读取文件
        FileInputStream is = new FileInputStream("C:\\Users\\王文龙\\Pictures\\Saved Pictures\\1.png");
        //2.获取response字节输出流
        ServletOutputStream os = response.getOutputStream();
        //3.完成流的copy
        /*byte[] buffer=new byte[1024];
        int len=0;
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
        }*/

        IOUtils.copy(is,os);

        //关闭流
        is.close();
    }
}

五、补充:路径知识

在这里插入图片描述

解决方法:使用动态获取虚拟路径

package org.example.web.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;

@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

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

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


        //重定向
       /* //1.设置响应状态码302
        response.setStatus(302);

        //2.设置响应头Location
        response.setHeader("Location","/request_demo/resp2");*/

        //简化方式完成重定向
        
        //动态获取虚拟目录
        String contextPath = request.getContextPath();
        
        response.sendRedirect(contextPath+"/resp2");
    }
}


总结

Tomcat对request和response要形成实现类并创建对象,并将参数传入到对应方法中(service方法中).
学会对response的基本方法的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值