本文总结自,B站-遇见狂神说
概念:代表整个web应用,可以和程序的容器(服务器)来通信
web容器在启动的时候,他会为每个web程序创建一个对应的ServletContext对象,它代表了当前的web应用
1. 常用方法
获取ServletContext
对象
- 通过
request
对象获取
request.getServletContext();
- 通过
HttpServlet
获取
this.getServletContext();
获取MIME类型
MIME类型:在互联网通信过程中定义的一种文件数据类型
- 格式: 大类型/小类型 如:text/html image/jpeg
String getMimeType(String file)
获取初始化参数
web.xml
<!--web应用初始化配置-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhostP:3306/mybatis</param-value>
</context-param>
Servlet
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
ServletContext context = this.getServletContext();
// 获取配置文件web.xml中的初始化配置
String url = context.getInitParameter("url");
response.getWriter().print(url);
}
结果:网页输出
jdbc:mysql://localhostP:3306/mybatis
2. 域对象:共享数据
ServletContext
对象范围:所有用户所有请求的数据
- 设置
setAttribute(String name,Object value)
- 获取
getAttribute(String name)
- 删除
removeAttribute(String name)
3. 获取文件的真实(服务器)路径
有一种方法,
chassLoader
:只能获取src
下的资源,web
下获取不到
方法:
ServletContext context = this.getServletContext();
- web目录下资源访问
String realPath = context.getRealPath("/b.txt");
- WEB-INF目录下的资源访问
String c = context.getRealPath("/WEB-INF/c.txt");
src
目录下的资源访问
String a = context.getRealPath("/WEB-INF/classes/a.txt");
结果:
E:\File\ideaProjects\JavaWeb\out\artifacts\09_Response_war_exploded\b.txt
E:\File\ideaProjects\JavaWeb\out\artifacts\09_Response_war_exploded\WEB-INF\c.txt
E:\File\ideaProjects\JavaWeb\out\artifacts\09_Response_war_exploded\WEB-INF\classes\a.txt
4. 请求转发
ServletContext context = this.getServletContext();
context.getRequestDispatcher("/s3").forward(request,response);
图解
特点:路径未改变!
A想拿到C的资源,但是A只能找到B
5. 读取资源文件
Properties
类
- 在java目录下新建properties
- 在resources目录下新建properties
发现都被打包到了同一路径下:classes
,这个路径俗称类路径
可能会产生配置文件无法加载的问题
解决:更改maven配置文件
<!--防止资源导出失败-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<!--java文件夹下-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
代码:
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
ServletContext context = this.getServletContext();
InputStream in = context.getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
// 输出到页面
response.getWriter().print(user+":"+pwd);
}
web.xml配置:设置映射即可
结果:
root:root
案例_文件下载
package cn.luis;
import javax.servlet.ServletOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class FileServlet extends javax.servlet.http.HttpServlet {
@Override
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
this.doGet(request, response);
}
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
// 1.获取下载文件路径
String realPath = "D:\\code\\Web\\HttpServletResponse\\target\\classes\\1.png";
// String realPath = this.getServletContext().getRealPath(path);
System.out.println("下载文件路径:" + realPath);
// 2.下载文件名是?
String name = realPath.substring(realPath.lastIndexOf("\\" + 1));
// 2.1中文文件名需要转码
String encodeName = URLEncoder.encode(name, "UTF-8");
// 3.让浏览器支持索要下载的东西
response.setHeader("Content-Disposition", "attachment;filename=" + encodeName);
// 4.获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
// 5.创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
// 6. 获取OutputStream输出流
ServletOutputStream out = response.getOutputStream();
// 7. 将FileOutPutStream流写入buffer缓冲区
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
// 8.释放资源
in.close();
out.close();
}
}