1 ServletContext—对象的概念
代表整个web应用,可以喝程序的容器(服务器)通信
2 ServletContext—对象的获取
(ServletContextDemo01)
2.1 获取方式
- 1、通过request对象获取:request.getServletContext();:
- 2、通过HttpServlet获取:this.getServletContext();
2.2 案例展示
3 ServletContext—功能-获取MIME类型
3.1 获取MIME类型
MIME类型:互联网通信过程中的一种文件数据类型
MIME格式:大类型/小类型 如:text/html image/jpg
MIME类型获取方法:String getMimeType(String file文件后缀名)
MIME类型位置:
3.2 获取MIME类型—案例
(ServletContextDemo02)
- 1、代码
package com.zzy.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;
/**
* ServletContext对象回去MIME类型
*/
@WebServlet("/servletContextDemo02")
public class ServletContextDemo02 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Servlet的功能 1 :获取MIME类型
* MIME类型:互联网通信过程中的一种文件数据类型
* MIME格式:大类型/小类型 如:text/html image/jpg
* MIME类型获取方法:String getMimeType(String file文件后缀名)
* */
//1.获取ServletContext对象
ServletContext context = this.getServletContext();
//2、定义文件名称
String fileName = "a.html";
//3、获取MIME类型
String mimeType = context.getMimeType(fileName);
System.out.println(mimeType);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
-
2、访问路径
http://localhost:8080/day15/servletContextDemo02 -
3、 访问结果
4 ServletContext—域对象(共享数据)
4.1 方法介绍
存值:setAttribute(String name,Onject value)
取值:getAttribute(String name)
删除值:removeAttribute(String name)
4.2 ServletContext对象的范围
4.2.1 编写案例—ServletContextDemo03/04
ServletContextDemo03
package com.zzy.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;
/**
* ServletContext域对象的方法和作用范围
*/
@WebServlet("/servletContextDemo03")
public class ServletContextDemo03 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* ServletContext域对象的作用范围
* 存值:setAttribute(String name,Onject value)**
* 取值:getAttribute(String name)**
* 删除值:removeAttribute(String name)**
* */
//1、获取ServletContext对象
ServletContext context = this.getServletContext();
//2、向域对象中存储数据
context.setAttribute("msg", "ServletContext");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
ServletContextDemo04
package com.zzy.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;
/**
* ServletContext域对象:获取域中的数据 删除域中的数据
*/
@WebServlet("/servletContextDemo04")
public class ServletContextDemo04 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取ServletContext域对象
ServletContext context = this.getServletContext();
//1、获取ServletContext域对象中的数据
String msg = (String) context.getAttribute("msg");
System.out.println("域对象中的数据是:---"+msg);
//2、将ServletContext域对象中的数据删除
context.removeAttribute("msg");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
4.2.2 访问路径
域中存储数据路径:http://localhost:8080/day15/servletContextDemo03
域中获取数据路径:http://localhost:8080/day15/servletContextDemo04
4.3.2 访问结果
5 ServletContext—获取文件的真实(服务器)路径
5.1 方法介绍
获取文件的真实路径的方法:
String .getRealPath(String path)
5.2 案例—ServletContextDemo05
5.2.1 获取不同目录下的文件真实路径
* 文件1:src目录下的a.txt文件
* 文件2:webapps(web)目录下的b.txt文件
* 文件3:WEB-INF目录下的c.txt文件**
5.2.1.1 文件1:src目录下的a.txt文件
5.2.1.2 文件2:webapps(web)目录下的b.txt文件
5.2.1.3 文件3:WEB-INF目录下的c.txt文件**
5.2.1.4 项目的根目录
其中 / 表示的是项目的根目录
5.2.2 代码展示
package com.zzy.servletcontext;
import javax.servlet.ServletContext;
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;
/**
* ServletContext域对象:获取域中的数据 删除域中的数据
*/
@WebServlet("/servletContextDemo05")
public class ServletContextDemo05 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取ServletContext域对象
ServletContext context = this.getServletContext();
/*
* 2.获取文件的真实路径
* 文件1:src目录下的a.txt文件
* 文件2:webapps(web)目录下的b.txt文件
* 文件3:WEB-INF目录下的c.txt文件
* */
//文件1:src目录下的a.txt文件,在Tomcat部署完项目之后,src下的文件粗出在WEB-INF目录下的classes文件夹内
String realPatha = context.getRealPath("/WEB-INF/classes/a.txt");
System.out.println(realPatha);
/*
* D:\workspaceIDEA08\01-workstudy\workspace-JavaWeb\dat15_response
* \out\artifacts\dat15_response_war_exploded
* \WEB-INF\classes\a.txt
* */
//文件2:webapps(web)目录下的b.txt文件
String realPathb = context.getRealPath("/b.txt");
System.out.println(realPathb);
/*
* D:\workspaceIDEA08\01-workstudy\workspace-JavaWeb\dat15_response
* \out\artifacts\dat15_response_war_exploded
* \b.txt
* */
//文件3:WEB-INF目录下的c.txt文件
String realPathc = context.getRealPath("/WEB-INF/c.txt");
System.out.println(realPathc);
/*
* D:\workspaceIDEA08\01-workstudy\workspace-JavaWeb\dat15_response
* \out\artifacts\dat15_response_war_exploded
* \WEB-INF\c.txt
* */
/*
* 注意点:
* 其中 / 表示:D:\workspaceIDEA08\01-workstudy\workspace-JavaWeb\dat15_response
* \out\artifacts\dat15_response_war_exploded
* */
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
5.2.3 访问路径
http://localhost:8080/day15/servletContextDemo05