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的更多相关文章

  1. ServletConfig与ServletContext

    ServletConfig与ServletContext对象详解 一.ServletConfig对象    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...

  2. JavaEE:Servlet简介及ServletConfig、ServletContext

    Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...

  3. day05 Servlet 开发和 ServletConfig 与 ServletContext 对象

    day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...

  4. JaveWeb学习之Servlet(二):ServletConfig和ServletContext

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...

  5. ServletConfig、ServletContext 的应用

    一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...

  6. JavaWeb学习笔记:ServletConfig()和ServletContext()

    ServletConfig()和ServletContext() 1.ServletConfig() ServletConfig是一个接口,它由server提供商来实现. ServletConfig封 ...

  7. ServletConfig和ServletContext 区别

      ServletConfig和ServletContext 1.ServletContext在整个web应用程序生命周期内存在,用来保存全局对象,整个web应用都可以使用其获取context参数.当 ...

  8. 谈谈 ServletConfig 和 ServletContext

    目录 一.ServletConfig 和 ServletContext 的概念 二.ServletConfig 和 SerlvetContext 代码表示 一.ServletConfig 和 Serv ...

  9. Servlet技术之——概述、实现、细节、获取资源、ServletConfig、ServletContext

    Servlet概述.实现.细节.获取资源.ServletConfig.ServletContext (一) Setvlet基本概述 (1) 什么是Servlet ? Servlet(Server Ap ...

随机推荐

  1. Android中对手机文件进行读写

    参考张泽华视频 (一)读写手机内存卡中的文件 对手机中的文件进行读写操作,或者新增一个文件时,可直接使用openFileOutput  /  openFileInput 得到文件的输出.输入流. Fi ...

  2. Bootstrap 静态分页 和 jquery_pagination插件 动态分页

    第一种Bootstrap 实例 - 默认的分页 <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - ...

  3. Python使用xslt提取网页数据

    1,引言 在Python网络爬虫内容提取器一文我们详细讲解了核心部件:可插拔的内容提取器类gsExtractor.本文记录了确定gsExtractor的技术路线过程中所做的编程实验.这是第一部分,实验 ...

  4. php 解决乱码的通用方法

    一,出现乱码的原因分析 1,保存文件时候,文件有自己的文件编码,就是汉字,或者其他国语言,以什么编码来存储 2,输出的时候,要给内容指定编码,如以网页的形势输入时<meta http-equiv ...

  5. MVC Controller 与 View 传值

    Controller 到 View 1 强类型 控制器 // GET: /Test/ public ActionResult Index() { DateTime date = DateTime.No ...

  6. VS2005快捷键

    VS2005快捷键 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT + C显 ...

  7. 对发给Application.Handle消息的三次执行(拦截)消息的过程

    unit Main; interface uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms ...

  8. Spring MVC对象转换说明

    在Spring MVC之前我们需要在Servlet里处理HttpServletRequest参数对象,但这个对象里的属性都是通用类型的对象(如字符串),处理起来很繁琐并且容易出错,而Spring MV ...

  9. UESTC_One Step Two Steps CDOJ 1027

    As we all know,the handsome boy, Zcat, has a fever of flat shoes. He sings it on the class, in the d ...

  10. cenos 安装 phpredis 扩展

    1. php -m 可以查看 php 所有的已经安装的扩展