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的更多相关文章
随机推荐
- openstack共享组件--rabbitmq消息队列(1)
一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 消息传 ...
- AutoResetEvent和ManualResetEvent(多线程操作)
摘自风中灵药的博客:https://www.cnblogs.com/qingyun163/archive/2013/01/05/2846633.html#!comments AutoResetEven ...
- java:redis(java代码操作redis,实体类mapper生成器(generator))
1.redis_demo Maven ItemMapper.xml: <?xml version="1.0" encoding="UTF-8" ?> ...
- 网络实验 03-交换机划分VLAN配置
交换机划分VLAN配置 一.实验目标 理解虚拟 LAN(VLAN)基本原理 掌握一般交换机按端口划分 VLAN的配置方法 掌握Tag VLAN配置方法 二.实验背景 某一公司内财务部.销售部的PC通过 ...
- XCTF (app2)
打开app,有两个输入框和一个按钮.点击按钮会跳转到新的页面显示Waiting for you. 打开JEB反编译. 如果两个输入框的长度都不为0,那么获取这两个值到v0和v1中,Log记录日志. 创 ...
- 【Git】工作中99%能用到的git命令
Git使用笔记 1.第一次使用github ============================================= 1)github注册账号 使用邮箱注册账号 先不要创建版本库 2 ...
- JAVA实验三及总结
JAVA第五周作业 Java实验报告三 第一题 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) (1).统计该字符 ...
- 自然语言处理工具pyhanlp分词与词性标注
Pyhanlp分词与词性标注的相关内容记得此前是有分享过的.可能时间太久记不太清楚了.以下文章是分享自“baiziyu”所写(小部分内容有修改),供大家学习参考之用. 简介 pyhanlp是HanLP ...
- 小菜鸟之Cisco
Switch>enable// Switch#configure// Switch#show vlan//展示vlan接口 Switch(config)#enable password 1234 ...
- oracle查询表的结构
SELECT t.table_name,t.column_name,t.data_type||'('||t.data_length||')', t1.comments FROM User_Tab_Co ...