08ServletContext
1. 概念
代表整个web应用,可以和程序的容器(服务器)来通信
2. 获取
1. 通过request对象获取
request.getServletContext();
2. 通过HttpServlet获取
this.getServletContext();
package cn.itcast.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; @WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* ServletContext对象获取:
1. 通过request对象获取
request.getServletContext();
2. 通过HttpServlet获取
this.getServletContext();
*/ //1. 通过request对象获取
ServletContext context1 = request.getServletContext();
//2. 通过HttpServlet获取
ServletContext context2 = this.getServletContext(); System.out.println(context1);
System.out.println(context2); System.out.println(context1 == context2);//true } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
3. 功能
1. 获取MIME类型:
MIME类型:在互联网通信过程中定义的一种文件数据类型
格式: 大类型/小类型 text/html image/jpeg
获取:String getMimeType(String file)
package cn.itcast.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; @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); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
2. 域对象:共享数据
1. setAttribute(String name,Object value)
2. getAttribute(String name)
3. removeAttribute(String name)
ServletContext对象范围:所有用户所有请求的数据
package cn.itcast.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; @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.itcast.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; @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);
}
}
3. 获取文件的真实(服务器)路径
方法:String getRealPath(String path)
package cn.itcast.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.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);
// File file = new File(realPath); String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
System.out.println(c); String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
System.out.println(a); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
08ServletContext的更多相关文章
随机推荐
- Pyqt5-QtWidget的使用
QTableWidget是QTableViewer的子类 ,其中QTableViewer可以使用自定义的数据模型来显示内容(通过setModel ()来绑定数据源),而QTableWidget提供了一 ...
- spring boot starter是什么
参考自:https://www.cnblogs.com/EasonJim/p/7615801.html Spring Boot中Starter是什么 比如我们要在Spring Boot中引入Web M ...
- linux常用命令(9)nl命令
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...
- Java多线程(3):wait()/notify()实例
下面是代码实例 public class WaitDemo implements Runnable { private Object lock; public WaitDemo(Object lock ...
- Sqlserver实现故障转移 — sqlserver镜像备份实现故障转移(3)
目的:在已经加域的计算机上安装sqlserver2012,并配置数据库镜像实时同步,并实现故障转移. 在数据库层面实现故障自动转移后,应用程序里改怎么写数据库连接呢?其实使用ADO.NET或者SQL ...
- 【Linux】常用基础命令
修改时间 date -s 月/日/年 例如:date -s 07/31/2019 date -s 时:分:秒 例如:date -s 23:56:50 hwclock -w 将时间写到bois,防止重启 ...
- HCL试验9
PC1配置: ip:192.168.1.1 掩码:255.255.255.0 网关:192.168.1.254 上路由器配置: sys int gi0/0 ip add 192.168.100.10 ...
- Python面试简介及并行并发
今天的分享内容大体如下: 一. 面试 1. 什么是面试 2. 优秀的面试 二. Python综述 1. Python设计哲学及版本变迁 2. Python发展现状及其他语言使用场景 3. GIL 4. ...
- android webview 访问 https 页面
在android 中利用webview 控件进行开发过程中,可能会遇到 webview 访问不了https://的页面如 https://www.google.com.hk 重写onReceivedS ...
- ubuntu查看目录大小
du -h --max-depth=1 该命令会查看目录下的所有子目录大小,以及目录总共占用磁盘空间