ServletConfig和ServletContext
ServletConfig和ServletContext
- Servlet初始化参数
在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servlet配置一些初始化参数
<servlet-name>ServletConfigDemo1</servlet-name>
<servlet-class>....</servlet-class>
....
<init-param>
<param-name>charest</param-name>
<param-value>UTF-8</param-value>
</init-param>
....
</servlet>
- 通过ServletConfig获取Servlet的初始化参数
要想获取Servlet的初始化参数,顾名思义,应该在初始化中得到 所以在init()中:
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
Servlet对象中含有ServletConfig对象,可以通过this.getServletConfig()获得(servletConfig)
而获取初始化参数,只需要调用servletConfig.getInitParameter("charest")
- 多个Servlet之间通过ServletContext共享数据
由于ServletConfig维护了ServletContext的引用,可以用 this.getServletConfig().getServletContext()获取ServletContext对象(context)
也直接this.getServletContext() 数据传输过程定义在doGet()中.
servlet1传入数据: context.setAttribute()
servlet2获取数据: context.getAttribute()
当然也要在servlet1运行之后servlet1才能取得共享数据
- 获取WEB应用的初始化参数
<display-name></display-name>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
通过ServletContext获取整个web网站的初始化参数
context.getInitParameter("url")
- ServletContext请求转发
RequestDispatcher rd = context.getRequestDispacher("/servlet/....");
rd.forward(request,response);
- ServletContext读取资源文件
使用ServletContext的getResourceAsStream()方法获取输入流对象
InputStream in = this.getServletContext().getResourceAsStream((String) uri);
然后将数据写进输入流
- 客户端缓存Servlet输出
在客户端进行合理的数据缓存,可以避免浏览器频繁的向服务器发送多余请求.
下面一段代码为将数据缓存到浏览器1天时间
String data = "reserved"; response.setDateHeader("expires",System.currentTimeMillis()+24 * 3600 * 1000); response.getOutputStream().write(data.getBytes());
ServletConfig和ServletContext的更多相关文章
- ServletConfig与ServletContext
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- day05 Servlet 开发和 ServletConfig 与 ServletContext 对象
day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...
- JaveWeb学习之Servlet(二):ServletConfig和ServletContext
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...
- ServletConfig、ServletContext 的应用
一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...
- JavaWeb学习笔记:ServletConfig()和ServletContext()
ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...
- ServletConfig和ServletContext 区别
ServletConfig和ServletContext 1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数.当 ...
- 谈谈 ServletConfig 和 ServletContext
目录 一.ServletConfig 和 ServletContext 的概念 二.ServletConfig 和 SerlvetContext 代码表示 一.ServletConfig 和 Serv ...
- Servlet技术之——概述、实现、细节、获取资源、ServletConfig、ServletContext
Servlet概述.实现.细节.获取资源.ServletConfig.ServletContext (一) Setvlet基本概述 (1) 什么是Servlet ? Servlet(Server Ap ...
随机推荐
- web 前端 shopnc项目 首页分类一开始做前端,我是拒绝的
看图别说话 经过几小时的折腾 主要还是靠耐心
- ansilbe 入门001、ansible的介绍
概述: ansible 作为一个配置管理工具.首先我们要“告诉”它管理的是那几台机器啊:而这个信息就在要ansible 的配置文件中体现了.默认情况下ansible的配置文件保存在 /etc/ansi ...
- BZOJ 2535 Plane 航空管制2
http://www.lydsy.com/JudgeOnline/problem.php?id=2535 思路:对于1,我们只需要每个点比前驱大就可以了,然后满足尽量优. 对于第二问,我们先求出这个点 ...
- SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据
原文:SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Se ...
- 转:C# 中的委托和事件
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- 【转】如何检测wifi信号强度? -- 不错
原文网址:http://jingyan.baidu.com/article/90895e0fe9616164ec6b0b88.html 当我们在使用wifi上网时,在某些角落会出现无wifi信号,或是 ...
- UESTC_导弹拦截 2015 UESTC Training for Dynamic Programming<Problem N>
N - 导弹拦截 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- 只允许指定IP访问指定端口 ufw
那天工作需要在服务器上指定ip才可以访问指定端口,配置命令如下: (服务器是ubuntu 14.04系统) apt-get install ufw ufw enable ufw default den ...
- poj 3436 (最大流)
题意:每台电脑共有p种零件,现在有n台机器,给出n台机器每台需要的一些种类零件当原料(0代表不需要,1代表必须要,2代表可有可无)和输出的产品零件.问怎么安排生产线使生产出来零件可以组装的电脑最多. ...
- Android 自己的自动化测试(4)<uiautomator>
在前面的系列文章.我与介绍java实现 Android 自己主动化測试(1)怎样安装和卸载一个应用(java).Android 自己主动化測试(2)依据ID查找对象(java):然后又介绍了用pyth ...