系列文章目录
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的基本方法的使用