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 ...
随机推荐
- SSH 概念及使用详解
注意:转载请注明出处:http://www.programfish.com/blog/?p=124 SSH 基本概念 SSH 英文全称是secure shell,字面意思:安全的shell. SSH协 ...
- 1009 Enigma
本题的重点是理解清楚题意并能代码模拟.形式是二战德国密码机,和数据结构.算法联系较少. #include <stdio.h> #include <string.h> int m ...
- JS--switch 语句
说明:js中switch语句的语法风格 function convert(x){ switch(x) { case "string": document.write("s ...
- BZOJ 1084 最大子矩阵
http://www.lydsy.com/JudgeOnline/problem.php?id=1084 思路:分m=1和m=2操作 #include<algorithm> #includ ...
- javascript之Function函数
在javascript里,函数是可以嵌套的. 如: function(){ funcrion square(x){ return x*x; } return square(10); } 在javas ...
- 关于Redis的一些常识
http://blog.csdn.net/mengxianhua/article/details/8961713 关于Redis的一些常识 2013-05-22 18:00 13315人阅读 评论(0 ...
- Python re模块 正则表达式
1 简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C ...
- iOS 7 改变Status Bar 颜色
Set the UIViewControllerBasedStatusBarAppearance to NO in the Info.plist. In ViewDidLoad method or a ...
- 手机端Zepto框架,利用swipejs插件做banner轮播图
一,HTML部分<div class="banner"> <div id="slider" class="swipe"&g ...
- kiki's game
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: ...