概念:代表整个 web 应用,可以和程序的容器(服务器)来通信。
ServletContext 代表是一个web应用的环境(上下文)对象,ServletContext 对象内部封装是该 web 应用的信息 ServletContext 对象一个 web 应用只有一个。
问题:一个 web 应用有几个 servlet 对象? ----多个
① 获得web应用全局的初始化参数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PIcdf7Z0-1602124049694)(/api/project/7821181/files/20940403/imagePreview)]
② 获得 web 应用中任何资源的绝对路径(重要) 方法:String path = context.getRealPath(相对于该web应用的相对地址);
③ ServletContext 是一个域对象(重要) 什么是域对象?什么是域? 存储数据的区域就是域对象
注: ServletContext域对象的作用范围:整个web应用(所有的web资源都可以随意向 servletcontext域中存取数据,数据可以共享)
域对象的通用的方法:
setAtrribute(String name,Object obj);getAttribute(String name);removeAttribute(String name); package cn.ys.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; @WebServlet("/servletContextDemo2") public class ServletContextDemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* ServletContext功能: 1. 获取MIME类型: * MIME类型:在互联网通信过程中定义的一种文件数据类型 * 格式: 大类型/小类型 text/html image/jpeg * 获取:String getMimeType(String file) 2. 域对象:共享数据 3. 获取文件的真实(服务器)路径 */ //2. 通过HttpServlet获取 ServletContext context = this.getServletContext(); //3. 定义文件名称 String filename = "a.jpg"; //image/jpeg //4.获取MIME类型 String mimeType = context.getMimeType(filename); System.out.println(mimeType);//image/jpeg } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } package cn.ys.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; @WebServlet("/servletContextDemo3") public class ServletContextDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* ServletContext功能: 1. 获取MIME类型: 2. 域对象:共享数据 3. 获取文件的真实(服务器)路径 */ //2. 通过HttpServlet获取 ServletContext context = this.getServletContext(); //设置数据 context.setAttribute("msg", "haha"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } package cn.ys.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; @WebServlet("/servletContextDemo4") public class ServletContextDemo4 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* ServletContext功能: 1. 获取MIME类型: 2. 域对象:共享数据 3. 获取文件的真实(服务器)路径 */ //2. 通过HttpServlet获取 ServletContext context = this.getServletContext(); //获取数据 Object msg = context.getAttribute("msg"); System.out.println(msg); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } package cn.ys.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.File; import java.io.IOException; @WebServlet("/servletContextDemo5") public class ServletContextDemo5 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* ServletContext功能: 1. 获取MIME类型: 2. 域对象:共享数据 3. 获取文件的真实(服务器)路径 */ // 通过HttpServlet获取 ServletContext context = this.getServletContext(); // 获取文件的服务器路径 String b = context.getRealPath("/b.txt");//web目录下资源访问 System.out.println(b);///Users/yangshuo/IdeaProjects/JavaWeb/1-1-Servlet-7-ServletContext/src/main/webapp/b.txt // File file = new File(realPath); String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问 System.out.println(c);///Users/yangshuo/IdeaProjects/JavaWeb/1-1-Servlet-7-ServletContext/src/main/webapp/WEB-INF/c.txt String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问 System.out.println(a);///Users/yangshuo/IdeaProjects/JavaWeb/1-1-Servlet-7-ServletContext/src/main/webapp/WEB-INF/classes/a.txt } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }download.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="/day15/img/1.jpg">图片1</a> <a href="/day15/img/1.avi">视频</a> <hr> <a href="/day15/downloadServlet?filename=九尾.jpg">图片1</a> <a href="/day15/downloadServlet?filename=1.avi">视频</a> </body> </html>register.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> /* 分析: 点击超链接或者图片,需要换一张 1.给超链接和图片绑定单击事件 2.重新设置图片的src属性值 */ window.onload = function(){ //1.获取图片对象 var img = document.getElementById("checkCode"); //2.绑定单击事件 img.onclick = function(){ //加时间戳 var date = new Date().getTime(); img.src = "/day15/checkCodeServlet?"+date; } } </script> </head> <body> <img id="checkCode" src="/day15/checkCodeServlet" /> <a id="change" href="">看不清换一张?</a> </body> </html>