ServletContext详解(1)
来源:互联网 发布:网络域名交易合同骗局 编辑:程序博客网 时间:2024/05/23 13:09
首先看看Servlet核心类图,看看ServletContext在其中的地位
1,什么是ServletContext?
当Servlet容器启动时,会为每个javaweb应用创建一个ServletContext对象的实例,SertvletContext属于全局对象(在应用被服务器加载时创建).每一个javaweb应用都有唯一的一个ServletContext对象.它代表着当前应用.
2,如何 得到ServletContext对象?
方式一:(api:TheServletContext
object is contained within theServletConfig
object.由服务器创建,可通过ServletConfig对象中类方法getServletContext()获取)
利用inti方法中的config形参
ServletConfig config;
public void init(ServletConfig config){
this.config=config;
}
ServletContext sc=config.getServletContext();
方式二:直接获取
getServletContext();
3.有什么用?
3.1.Servlet对象可以通过ServletContext来实现通信,因此ServletContext对象也被称为Context域对象(就是说其内部维护了一个Map<String,Object>)
例:
Object getAttribute(String name):根据名称获取绑定的对象
Enumeration getAttributeNames() :获取ServletContext域中的所有名称
void removeAttribute(String name):根据名称移除对象
void setAttribute(String name,Object value):添加或修改对象。
以上方法可以实现多个Servlet之间数据的共享
3.2获取web应用的初始化参数(全局参数)-----全局参数配置方法:在web.xml根元素下添加<context-param>
<param-name>****</param-name>
<param-value>****</param-value>
</context-param>
通过String sc.getInitParamerter(String name);
Enumeration sc.getInitParameterNames()获取
3.3实现Servlet的转发
(过后再谈)
3.4利用ServletContext对象读取资源文件
3.4.1文件的下载(ServletContext在这里的作用就是获取文件的真实路径)
代码如下:
private void xiazai(HttpServletResponse response)throws FileNotFoundException, IOException {
//得到要下载文件
ServletContext sc=getServletContext();
String path=sc.getRealPath("/j.png");//读取应用中任何文件。只能在Web环境下用
System.out.println(path);
//构建输入流
FileInputStream in =new FileInputStream(path);
//通知客户端以下载的方式打开
response.setHeader("Content-Disposition", "attachment;filename=j.png");
response.setHeader("Content-type", "application/octet-stream");
//得到shu出流
ServletOutputStream out=response.getOutputStream();
int len=-1;
byte[] b=new byte[1024];
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close();
}
//下载中文名的文件
private void xiazai2(HttpServletResponse response)
throws FileNotFoundException, IOException {
//得到要下载文件
ServletContext sc=getServletContext();
String path=sc.getRealPath("/美女.png");
String filename=path.substring(path.lastIndexOf("\\")+1);
System.out.println(path);
//构建输入流
FileInputStream in =new FileInputStream(path);
//通知客户端以下载的方式打开
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));//一定要URL编码
response.setHeader("Content-type", "application/octet-stream");
//得到shu出流
ServletOutputStream out=response.getOutputStream();
int len=-1;
byte[] b=new byte[1024];
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close();
}
- ServletContext详解(1)
- ServletContext详解
- ServletContext详解
- ServletContext详解
- ServletContext 详解
- ServletContext 详解
- ServletContext详解
- ServletContext详解
- ServletContext详解
- ServletContext详解
- ServletContext详解
- ServletContext详解
- ServletContext作用功能详解
- Java ServletContext 详解
- ServletContext作用功能详解
- Java ServletContext 详解
- Java ServletContext 详解
- ServletContext作用功能详解
- mybatis 一次执行多条SQL
- Javascript中函数和作用域
- 【框架】JitPack---如何拥有自己的动态库
- mycat 使用mysql实现全局序列号
- sublime 直接运行php代码
- ServletContext详解(1)
- ListView相关
- 什么是Word密码破解工具字典攻击
- Android少踩的坑
- Android CTS
- Spring IOC注入(二)构造器注入
- ASCII编码对照表
- root登录ubuntu14.01
- Nginx学习之三-ngx_http_request_t结构体