Java第四十二天,Http协议,Response
一、Response 功能
- 设置响应行
- 设置响应头
- 设置响应体
二、Response 使用
1. 设置响应行
设置状态码
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(状态码);
}
2.设置响应头
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("键", "值");
}
3. 设置响应体
(1)获取输出流
// 字符流
PrintWriter writer = httpServletResponse.getWriter();
// 字节流
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
(2)使用输出流,将数据输出到客户端浏览器
字符流
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
// 设置编码格式(有了 httpServletResponse.setHeader("content-type", "text/html;charset=utf-8"); 之后本句可以省略)
httpServletResponse.setCharacterEncoding("utf-8");
// 告诉客户端浏览器 服务器发送数据的编码,建议客户端浏览器使用指定的编码格式解码
httpServletResponse.setHeader("content-type", "text/html;charset=utf-8");
// 简化写法
// httpServletResponse.setContentType("text/html;charset=utf-8");
PrintWriter writer = httpServletResponse.getWriter();
writer.write("这是我的第一个页面");
}
字节流
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
// 设置编码格式(有了 httpServletResponse.setHeader("content-type", "text/html;charset=utf-8"); 之后本句可以省略)
httpServletResponse.setCharacterEncoding("utf-8");
// 告诉客户端浏览器 服务器发送数据的编码,建议客户端浏览器使用指定的编码格式解码
httpServletResponse.setHeader("content-type", "text/html;charset=utf-8");
// 简化写法
// httpServletResponse.setContentType("text/html;charset=utf-8");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
outputStream.write("太恐怖了".getBytes("utf-8"));
}
三、绝对路径与相对路径
1.绝对路径
(1)定义:
以 / 开头的路径称为绝对路径,通过绝对路径,可以确定唯一资源
(2)规则:
找到当前资源和目标资源之间的相对位置关系
2.相对路径
(1)定义
以 . 开头的路径成为相对路径,通过相对路径,不可以确定唯一资源;要使用相对路径,需要找到当前资源和目标资源之间的相对位置关系
(2)规则
判断定义的路径是给谁用的?判断请求将来从哪儿发出
- 给客户端浏览器使用:需要加虚拟目录(项目的访问路径),建议动态获取
建议虚拟目录动态获取:servletRequest.getContextPath()
- 给服务器使用:不需要加虚拟目录
3.注意
相对路径开头的的 ./ (是 点 和 正斜线)可以省略,即路径前如果没有 / ,则代表该路径是相对路径
四、ServletContext
1.概念
代表整个 web 应用,可以和 web 的容器(比如 Tomcat )进行通信
2.功能
- 获取 MIME 类型
- 作为一个域对象,可以共享数据
- 获取文件的真实路径
3.获取方式
- httpServletRequest.getServletContext()
- HttpServlet.getServletContext()
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
// 方法一
ServletContext servletContext = httpServletRequest.getServletContext();
//方法二
// this.getServletContext() 本质上是 HttpServlet.getServletContext();
ServletContext servletContext = this.getServletContext();
}
4.获取 MIME 类型
MIME:在互联网通信过程中定义的一种文件数据类型;格式为:大类型/小类型,例如:text/html、image/jpeg
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
String fileName = "my.png";
ServletContext servletContext = this.getServletContext();
String mimeType = servletContext.getMimeType(fileName);
System.out.println(mimeType);
}
5.共享数据
(1)方法
ServletContext.setAttribute(String name,Object value)
ServletContext.getAttribute(String name)
ServletContext.removeAttribute(String name)
(2)域对象作用范围
整个 web 应用
(3)举例
共享者
import javax.servlet.ServletContext;
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(urlPatterns = {"/first"})
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("key", "value");
httpServletResponse.sendRedirect("/test/second");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
共享接收者
import javax.servlet.*;
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("/second")
public class SecondServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.write(this.getServletContext().getAttribute("key").toString());
}
}
6.共享文件
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
// config.xml 文件是 src 目录下的文件,该目录下的所有文件最终会被放到 web/WEB-INF/classes 目录下
// / 代表该项目下的 web 目录
String realPath = httpServletRequest.getServletContext().getRealPath("config.xml");
System.out.println(realPath);
}