> 文档中心 > Http协议以及Servlet中request方法和response方法的介绍

Http协议以及Servlet中request方法和response方法的介绍


前言   🎄:CSDN的小伙伴们大家好,今天跟大家分享Http协议以及Servlet中request方法和response方法的介绍,如果这篇文章对你有用,麻烦给我点个小赞以示鼓励吧🎄
  🏡:博客主页:空山新雨后的java知识图书馆
  ☁️:今天天气多云,适合学习
  📝:名言分享:要在这个世界上获得成功,就必须坚持到底:至死都不能放手。——伏尔泰📝
  📖上一篇文章:web服务器软件Tomcat的以及Servlet的学习📖
  👏欢迎大家一起学习,进步。加油👊
在这里插入图片描述


📖

文章目录

  • 一、HTTP
    • 1.1、概念
    • 1.2、特点
    • 1.3、历史版本
    • 1.4、请求消息数据格式
      •   1.4.1、请求行
      •   1.4.2、请求头
      •   1.4.3、请求空行
      •   1.4.4. 请求体(正文)
    • 1.5、http响应数据格式
      •   1.5.1、响应行
      •   1.5.2. 响应头:
      •   1.5.3. 响应空行
      •   1.5.4. 响应体:传输的数据
  • 二、Request对象
    • 2.1、request对象的概念以及他的原理
    • 2.2、request对象的功能以及其方法
      •    2.2.1、获取请求数据
      •   2.2.2、 请求转发:一种在服务器内部的资源跳转方式
      •   2.2.3、 共享数据:
    • 案例演示:防盗链
  • 三、response对象
    • 3.1、功能:设置响应消息
    • 3.2、重定向
    • 面试题:forward 与 redirect的区别(重定向与转发的特点)
    • 3.3、服务器输出字符数据到浏览器
    • 3.4、服务器输出字节数据到浏览器
  • 四、ServletContext对象

📖


一、HTTP

1.1、概念

Hyper Text Transfer Protocol :超文本传输协议**传输协议**:定义了客户端和服务器端通信时,发送数据的格式

在这里插入图片描述

1.2、特点

1、基于TCP/IP的高级协议
2、默认端口号:80
3、基于请求/响应模型的:一次请求对应一次响应
4、无状态的,每次请求之间相互独立,不能交互数据

1.3、历史版本

1.0:每一次请求响应都会建立新的连接,浪费资源
1.1:复用连接,资源可以重复利用

1.4、请求消息数据格式

  1.4.1、请求行

【请求方式 请求url 请求协议/版本】

例如:【GET /login.html HTTP/1.1】
  * 请求方式:HTTP协议有7中请求方式,常用的有2种

GET:
1. 请求参数在请求行中,在url后。
2. 请求的url长度有限制的
3. 不太安全
POST:
1. 请求参数在请求体中
2. 请求的url长度没有限制的
3. 相对安全

  1.4.2、请求头

客户端浏览器告诉服务器一些信息
【请求头名称 :请求头值】

  • 常见的请求头:
    1. User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息
     * 可以在服务器端获取该头的信息,解决浏览器的兼容性问题
    2. Referer:http://localhost/login.html
     * 告诉服务器,我(当前请求)从哪里来?
      * 作用:
        1. 防盗链:
        2. 统计工作:

  1.4.3、请求空行

空行,就是用于分割POST请求的请求头,和请求体的。

  1.4.4. 请求体(正文)

  • 封装POST请求消息的请求参数的

在这里插入图片描述

  字符串格式:

POST /login.html HTTP/1.1 Host: localhost User-Agent:
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101
Firefox/60.0 Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language:
zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate Referer:
http://localhost/login.html Connection: keep-alive
Upgrade-Insecure-Requests: 1
username=zhangsan

在这里插入图片描述

1.5、http响应数据格式

响应消息:服务器端发送给客户端的数据
数据格式

  1.5.1、响应行

1、组成:
协议/版本 响应状态码 状态码描述 2、响应状态码
服务器告诉客户端浏览器本次请求和响应的一个状态
状态嘛都是三位数字组成
状态码分类
1. 1xx:服务器就收客户端消息,但没有接受完成,等待一段时间后,发送1xx多状态码
2. 2xx:成功。代表:200
3. 3xx:重定向。代表:302(重定向),304(访问缓存)
4. 4xx:客户端错误。
* 代表:
* 404(请求路径没有对应的资源)
* 405:请求方式没有对应的doXxx方法
5. 5xx:服务器端错误。代表:500(服务器内部出现异常)

  1.5.2. 响应头:

  1. 格式:头名称: 值
    2. 常见的响应头:
    1. Content-Type:服务器告诉客户端本次响应体数据格式以及编码格式
    2. Content-disposition:服务器告诉客户端以什么格式打开响应体数据
    * 值:
    * in-line:默认值,在当前页面内打开
    * attachment;filename=xxx:以附件形式打开响应体。文件下载

  1.5.3. 响应空行

  1.5.4. 响应体:传输的数据

响应字符串格式

HTTP/1.1 200 OK Content-Type: text/html;charset=UTF-8
Content-Length: 101 Date: Wed, 06 Jun 2018 07:08:42 GMT

            $Title$            hello , response     

在这里插入图片描述

二、Request对象

  

2.1、request对象的概念以及他的原理

  1. request和response对象是由服务器创建的。我们来使用它们
  2. request对象是来获取请求消息,response对象是来设置响应消息
    这一节我们学习request对象,request对象的原理:
    当用户访问一个网页时,用户浏览器发出了一条请求路径到服务器中,Tomcat服务器根据请求消息中的URL路径,创建对应的Servlet对象,并且创建好了request对象和response对象,Tomcat服务器将两个对象传递给Service方法,调用Service方法,开发者就可以在service方法中通过request方法获取请求数据,并且通过response方法设置响应数据消息,最后由服务器将设置好的相应数据在响应个客户端浏览器。

2.2、request对象的功能以及其方法

   2.2.1、获取请求数据

1、获取请求行数据
* GET /day14/demo1?name=zhangsan HTTP/1.1

  • 方法:
    1. 获取请求方式 :GET
    * String getMethod()
    2. ()获取虚拟目录:/day14
    * String getContextPath()
    3. 获取Servlet路径: /demo1
    * String getServletPath()
    4. 获取get方式请求参数:name=zhangsan
    * String getQueryString()
    5. (
    )获取请求URI:/day14/demo1
    * String getRequestURI(): /day14/demo1
    * StringBuffer getRequestURL() :http://localhost/day14/demo1
    * URI:统一资源标识符 : /day14/demo1 共和国
    * URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国
    6. 获取协议及版本:HTTP/1.1
    * String getProtocol()
    7. 获取客户机的IP地址:
    * String getRemoteAddr()

代码实例

package com.study2.web.request;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;/** * @author wang * @version 1.0 * @packageName com.study2.web.request * @className RequestDemo1 * @date 2022/3/8 16:08 * @Description request请求行数据演示 */@WebServlet("/request")public class RequestDemo1 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /*1. 获取请求方式 :GET  * String getMethod() 2. (*)获取虚拟目录:/day14  * String getContextPath() 3. 获取Servlet路径: /demo1  * String getServletPath() 4. 获取get方式请求参数:name=zhangsan  * String getQueryString() 5. (*)获取请求URI:/day14/demo1  * String getRequestURI():/day14/demo1  * StringBuffer getRequestURL()  :http://localhost/day14/demo1* URL:统一资源定位符 : http://localhost/day14/demo1中华人民共和国* URI:统一资源标识符 : /day14/demo1共和国 6. 获取协议及版本:HTTP/1.1  * String getProtocol() 7. 获取客户机的IP地址:* String getRemoteAddr()*/ //1. 获取请求方式 :GET String method = req.getMethod(); System.out.println(method); // 2. (*)获取虚拟目录:/day14 String contextPath = req.getContextPath(); System.out.println(contextPath); //3. 获取Servlet路径: /demo1 String servletPath = req.getServletPath(); System.out.println(servletPath); //4. 获取get方式请求参数:name=zhangsan String queryString = req.getQueryString(); System.out.println(queryString); // 5. (*)获取请求URI:/day14/demo1 String requestURI = req.getRequestURI(); System.out.println(requestURI); StringBuffer requestURL = req.getRequestURL(); System.out.println(requestURL); //6. 获取协议及版本:HTTP/1.1 String protocol = req.getProtocol(); System.out.println(protocol); //7. 获取客户机的IP地址: String remoteUser = req.getRemoteUser(); System.out.println(remoteUser);    }}//GET/////request//null///request//http://localhost:8080/request//HTTP/1.1//null

在这里插入图片描述

2. 获取请求头数据

  • 方法: * (*)String getHeader(String name):通过请求头的名称获取请求头的值 * Enumeration getHeaderNames():获取所有的请求头名称

代码实例:

package com.study2.web.request;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.util.Enumeration;/** * @author wang * @version 1.0 * @packageName com.study2.web.request * @className RequestDemo2 * @date 2022/3/8 16:48 * @Description 获取请求头数据的测试 */@WebServlet("/request2")public class RequestDemo2 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取所有请求头名称 Enumeration<String> headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()) {     String name = headerNames.nextElement();     //根据请求头名称获取值     String value = req.getHeader(name);     System.out.println(name + "===" + value); }    }}//host===localhost:8080// connection===keep-alive// cache-control===max-age=0// sec-ch-ua===" Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"// sec-ch-ua-mobile===?0// sec-ch-ua-platform==="Windows"// upgrade-insecure-requests===1// user-agent===Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36// accept===text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9//sec-fetch-site===none//sec-fetch-mode===navigate//sec-fetch-user===?1//sec-fetch-dest===document//accept-encoding===gzip, deflate, br//accept-language===zh-CN,zh;q=0.9//cookie===Idea-5bd4a107=1807057a-f445-4216-a59e-5992fb9488c7

3. 获取请求体数据:
* 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
* 步骤:

  1. 获取流对象
    * BufferedReader getReader():获取字符输入流,只能操作字符数据
    * ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据

  2. 再从流对象中拿数据

在这里插入图片描述

  2.2.2、 请求转发:一种在服务器内部的资源跳转方式

  1. 步骤:
  1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
  2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
  1. 特点:
  1. 浏览器地址栏路径不发生变化
  2. 只能转发到当前服务器内部资源中。
  3. 转发是一次请求

  2.2.3、 共享数据:

  • 域对象:一个有作用范围的对象,可以在范围内共享数据 * request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
  • 方法:
    1. void setAttribute(String name,Object obj):存储数据
    2. Object getAttitude(String name):通过键获取值
    3. void removeAttribute(String name):通过键移除键值对 * 注意:这个放于forward方法的上面,不然响应太快会导致无法获取信息! 演示代码:
package com.study2.web.request;import javax.servlet.RequestDispatcher;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;/** * @author wang * @version 1.0 * @packageName com.study2.web.request * @className RequestDemo8 * @date 2022/3/9 8:38 * @Description 请求转发演示 */@WebServlet("/request8")public class RequestDemo8 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("requestDemo8被调用了");/* 3. 共享数据:* 域对象:一个有作用范围的对象,可以在范围内共享数据  * request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据  * 方法: 1. void setAttribute(String name,Object obj):存储数据 2. Object getAttitude(String name):通过键获取值 3. void removeAttribute(String name):通过键移除键值对  * 注意:这个放于forward方法的上面,不然响应太快会导致无法获取信息!*/ req.setAttribute("message", "hello data"); //演示请求转发 RequestDispatcher requestDispatcher = req.getRequestDispatcher("/request9"); requestDispatcher.forward(req, resp);    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    }}
package com.study2.web.request;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;/** * @author wang * @version 1.0 * @packageName com.study2.web.request * @className RequestDemo9 * @date 2022/3/9 8:39 * @Description 请求转发的演示、 */@WebServlet("/request9")public class RequestDemo9 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Object message = req.getAttribute("message"); System.out.println(message); System.out.println("requestDemo9被调用了");    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    }}

在这里插入图片描述

案例演示:防盗链

一般的站点或者静态资源托管站点都提供防盗链的设置,也就是让服务端识别指定的Referer,在服务端接收到请求时,通过匹配referer头域与配置,对于指定放行,对于其他referer视为盗链。

我们可以使用一个案例来模拟防盗链的概念

package com.study2.web.request;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;/** * @author wang * @version 1.0 * @packageName com.study2.web.request * @className RequestDemo4 * @date 2022/3/8 17:05 * @Description */@WebServlet("/request4")public class RequestDemo4 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //演示获取请求头数据 referer String referer = req.getHeader("referer"); System.out.println(referer); //演示防盗链 if (referer != null) {     if (referer.contains("servlet")) {  resp.setContentType("text/html;charset=utf-8");  resp.getWriter().write("正常播放数视频中。。。。");     } else {  resp.setContentType("text/html;charset=utf-8");  resp.getWriter().write("非正常播放,掐断播放。。。。");     } }    }}/** http://localhost/http://localhost:8080/servlet/test.htmlhttp://localhost/http://localhost:8080/servlet/test.html*/
<html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><form action="./demo2" method="post">    <input name="username">    <input type="submit" value="提交"></form><a href="http://localhost:8080/servlet/request4">点我播放视屏</a></body></html>
<html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><a href="http://localhost:8080/servlet/request4">高清电影</a></body></html>

视频效果演示

request获取请求头referer演示视频——防盗链

在这里插入图片描述

三、response对象

3.1、功能:设置响应消息

  1. 设置响应行

    1. 格式:HTTP/1.1 200 ok
    2. 设置状态码:setStatus(int sc)
  2. 设置响应头:setHeader(String name, String value)

  3. 设置响应体: * 使用步骤:
    1. 获取输出流
    * 字节输出流:ServletOutputStream getOutputStream()
    * 字符输出流:PrintWriter getWriter()
    2. 使用输出流,将数据输出到客户端浏览器

3.2、重定向

解释:当用户访问A资源时,A资源解决不了用户的问题,但是B资源可以解决,于是服务器就会响应一个状态码302,告诉浏览器重定向,并且告诉浏览器B资源的路径,响应头location就是B资源的路径,用户浏览器拿着这个路径再次向B资源发起请求,B资源响应给用户浏览器,这就是重定向的一个过程,他也是一个资源跳转的方式
重定向的特点:redirect

  1. 地址栏发生变化
  2. 重定向可以访问其他站点(服务器)的资源
  3. 重定向是两次请求。不能使用request对象来共享数据

在这里插入图片描述

面试题:forward 与 redirect的区别(重定向与转发的特点)

重定向的特点:redirect
1. 地址栏发生变化
2. 重定向可以访问其他站点(服务器)的资源
3. 重定向是两次请求。不能使用request对象来共享数据
转发的特点:forward
1. 转发地址栏路径不变
2. 转发只能访问当前服务器下的资源
3. 转发是一次请求,可以使用request对象来共享数据

重定向代码实例:

package com.study.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;/** * @author wang * @version 1.0 * @packageName com.study.web.response * @className ResponseDemo1 * @date 2022/3/9 20:13 * @Description 重定向演示 */@WebServlet("/responseDemo1")public class ResponseDemo1 extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //* 重定向:资源跳转的方式/* ////1. 设置状态码为302 resp.setStatus(302); // //2.设置响应头location resp.setHeader("location","/response/responseDemo2");*/ //简化版本的重定向代码 //访问的为本地的资源 //动态的访问本地资源 String contextPath = req.getContextPath(); resp.sendRedirect(contextPath + "/responseDemo2"); //访问服务器外的站点// resp.sendRedirect("https://www.baidu.com"); System.out.println("demo1======");    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp);    }}
package com.study.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;/** * @author wang * @version 1.0 * @packageName com.study.web.response * @className ResponseDemo1 * @date 2022/3/9 20:13 * @Description 重定向演示 */@WebServlet("/responseDemo2")public class ResponseDemo2 extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("demo2......======");    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp);    }}

3.3、服务器输出字符数据到浏览器

package com.study.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;/** * @author wang * @version 1.0 * @packageName com.study.web.response * @className ResponseDemo4 * @date 2022/3/10 9:47 * @Description 测试服务器输出字符流数据到浏览器 */@WebServlet("/responseDemo4")public class ResponseDemo4 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp);    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取流对象之前,设置编码格式,这种方式虽然能解决问题,但是是在知到浏览器使用的编码集的情况下 //因此,如果在不知道编码集的情况下,去设置也有可能出现乱码,因此,该方式一般不使用// resp.setCharacterEncoding("GBK");// resp.setCharacterEncoding("utf-8"); //告诉浏览器,服务器响应消息体的数据编码,并且建议浏览器也使用该编码 resp.setHeader("Content-type", "text/html;charset=utf-8"); //简便的方法,记住这个方法即可 resp.setContentType("text/html;charset=utf-8"); //获取流对象 PrintWriter pw = resp.getWriter(); //输出数据// pw.write("hello response");// pw.println("hello response2"); //解决中文乱码问题// pw.write("你好,response");//???response //浏览器也可以识别HTML标签并且自动解析 pw.write("

你好,response

"
); }}

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

package com.study.web.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;/** * @author wang * @version 1.0 * @packageName com.study.web.response * @className ResponseDemo5 * @date 2022/3/10 10:11 * @Description 测试使用字节流输出数据 */@WebServlet("/responseDemo5")public class ResponseDemo5 extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置编码 resp.setContentType("text/html;charset=utf-8"); //获取字节流数据 ServletOutputStream os = resp.getOutputStream(); //输出字节流数据// os.write("hello".getBytes()); //输出中文。字节流默认不会乱码,但是如果指定字符集,与浏览器解码字符集不一致,仍旧会乱码// os.write("你好".getBytes()); os.write("你不好".getBytes("utf-8"));//浣犱笉濂� //解决办法同字符流一致,设置相应字符集,并且告诉浏览器,注意,该方法要放在获取流对象之前    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp);    }}

在这里插入图片描述

四、ServletContext对象

1. 概念:代表整个web应用,可以和程序的容器(服务器)来通信
2. 获取:

  1. 通过request对象获取 request.getServletContext();
  2. 通过HttpServlet获取 this.getServletContext();

3. 功能:

  1. 获取MIME类型: * MIME类型:在互联网通信过程中定义的一种文件数据类型
    * 格式: 大类型/小类型 text/html image/jpeg * 获取:String getMimeType(String file)
    1. 域对象:共享数据
      1. setAttribute(String name,Object value)
      2. getAttribute(String name)
      3. removeAttribute(String name) * ServletContext对象范围:所有用户所有请求的数据
    2. 获取文件的真实(服务器)路径
      1. 方法:String getRealPath(String path)

代码演示

package com.study.web.servletContext;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;/** * @author wang * @version 1.0 * @packageName com.study.web.servletContext * @className ServletContextDemo3 * @date 2022/3/10 15:05 * @Description ServletContext功能测试,获取资源的真实路径 */@WebServlet("/context5")public class ServletContextDemo5 extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); //web目录下的资源访问 String a = context.getRealPath("/a.txt"); System.out.println(a); //E:\javaStudy\webProject\out\artifacts\Response_war_exploded\a.txt //web_info目录下的资源访问 String b = context.getRealPath("/WEB-INF/b.txt"); System.out.println(b); //E:\javaStudy\webProject\out\artifacts\Response_war_exploded\WEB-INF\b.txt //src目录下的资源访问 String c = context.getRealPath("/WEB-INF/classes/c.txt"); System.out.println(c); //E:\javaStudy\webProject\out\artifacts\Response_war_exploded\WEB-INF\classes\c.txt    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp);    }}