servletConfig

Servlet容器初始化一个servlet对象时,会为这个servlet对象创建一个servletConfig对象,该对象中包含了servlet的<init-param>初始化参数信息。Servlet容器在调用servlet对象的init(ServletConfig config)方法时,会把servletConfig对象当做参数传递给servlet对象。Init(ServletConfig config)方法中通过this.config=config将ServletConfig对象保存,之后在service方法中可以通过this.getServletConfig()获取该对象。如果要覆盖 init() 方法,应调用 super.init() 以确保该Servlet得到正确地初始化。

作用:将数据库信息、编码方式等配置信息放在web.xml中,以后修改时不需要修改源代码

常用方法:

  • String getServletName() ,获取当前Servlet在web.xml中配置的名字
  • String getInitParameter(String name),获取当前Servlet指定名称的一个初始化参数的值
  • Enumeration getInitParameterNames() ,获取当前Servlet所有初始化参数的名字组成的枚举
  • ServletContext getServletContext() ,获取代表当前web应用的ServletContext对象

一般开发者创建的Servlet都继承HttpServlet,而HttpServlet是GenericServlet的子类。GenericServlet也实现了getInitParameter()方法,因此Servlet可以直接调用该方法去获取servlet的配置信息,不用通过ServletCofig对象。

ServletContext

容器启动后,会为每一个Web应用创建唯一的一个符合ServletContext接口要求的对象---servlet context。只要不关闭服务器或删除web应用,该servlet context就一直存在。

作用:Web应用范围内存取共享数据;访问web应用的资源文件;Servlet对象之间通过ServletContext对象来实现通讯;获取Servlet容器的相关信息;访问日志信息等。

GenericServlet/ServletConfig/HttpSession/FilterConfig都提供了getServletContext()方法获取ServletContext对象。

绑定数据时,在满足使用条件的情况下,优先使用生命周期短的,提高内存使用率:

request<session<Context

常用方法:

  • getAttribute(String  name) ,获取绑定在servlet context上的数据。
  • getInitParameter(String name) ,获取<context-param>标签中为整个Web应用配置的初始化参数。
  • getInitParameterNames() ,一次性获取<context-param>里所有的初始化参数名
  • getRealPath(String path) ,获取应用程序内指定资源的绝对路径。
  • getResource(String parh),path必须是/开头,代表当前web应用程序的根目录。返回一个代表某个资源的URL对象。
  • getResoutceAsStream(String parh),可以使用相对于根目录的路径访问到web目录下的所有文件,而不必知道绝对路径,返回文件流。
//一次性获取Context里所有的初始化参数
Enumeration enumeration = context.getInitParameterNames();
while (enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
String value = context.getInitParameter(name);
System.out.println(name + ";" + value);
}
//通过ServletContext加载资源
ServletContext.getRealPath("/" )  //方法得到Web应用程序的根目录的绝对路径。
String path= this.getServletContext().getRealPath("config.properties");
Properties prop = new Properties(); // 注意导的包是import java.util.Properties;
prop.load(new FileReader(path));

常用概念区分:

  • 请求参数 parameter:浏览器发送过来的请求中的参数信息
  • 初始化参数 initparameter:在web.xml中为Servlet或ServletContext配置的初始化信息
  • 域属性 attribute:四大作用域中存取的键值对

servlet context 和 servlet config的更多相关文章

  1. Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

    错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...

  2. NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

    今天调试SSM框架项目后台JSOn接口,报出来一个让人迷惑的错误:NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config 上网查了一下别人的博 ...

  3. springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config

    springMVC开发web的时候,报错:java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 原因:未引入jstl ...

  4. 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix

    严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...

  5. Spring的servlet context和application context

    Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml def ...

  6. Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config解决

    出现这个问题往往伴随  HTTP-500错误 报错信息: HTTP Status - Handler processing failed; nested exception is java.lang. ...

  7. 报错!!!Servlet.service() for servlet [action] in context with path [/myssh] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException

    这个为什么报错啊~~ at com.hsp.basic.BasicService.executeQuery(BasicService.java:33) 这个对应的语句是   Query query = ...

  8. Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Unable to compile class for JSP] with root cause java.lang.IllegalArgumentException: Page directive: inval

    严重: Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Una ...

  9. INFO Dispatcher:42 - Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir

    INFO Dispatcher:42 - Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax ...

随机推荐

  1. html中的Flash对象

    开源Flash播放器 http://www.open-open.com/ajax/Video.htm

  2. Node.js 安装配置介绍

    Node.js 安装配置 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. 本安装教程以Node.js v6.10.1 LTS(长期支持版本)版本为例. Node.js安装 ...

  3. iOS图片填充UIImageView(contentMode)

    本文主要形象的介绍一下UIView的contentMode属性: 核心代码 [self.prp_imageView setContentMode:UIViewContentModeScaleAspec ...

  4. ado.net知识整理

    对ado.net总是半知半解,五大对象也总是混淆,近期自己做小项目练手,整理了一些知识点 ado.net的无要素(摘自其他博文) Connection 物件    Connection 对象主要是开启 ...

  5. cooking构建工具报错MSBUILD :error MSB4132解决办法

    最近学习cooking构建工具的时候,在自己的笔记本上运行的好好的,项目在公司电脑上clone下来的时候,发现构建报错,逐条查错,试了好多方法也不行 最后在github上找到了答案,只是之前一直没找到 ...

  6. Use “error_messages” in Rails 3.2? (raises “undefined method” error)

    I am getting the following error in my Rails 3.2 functional tests: ActionView::Template::Error: unde ...

  7. Java容器的各种总结

    Java容器指的是List,Set,Map这些类.由于翻译的问题,问到集合,Collection这些指的都是它们几个. List ArrayList 随机访问快 LinkedList 插入删除快 这个 ...

  8. juery悬浮框

    现在的淘宝啊,京东啊等很多平台都用到了一个技术,就是当页面下拉时,某个div会一直悬浮在页面顶端.具体代码如下<p>jQuery实现页面滚动时层智能浮动定位</p><!D ...

  9. 《C++之那些年踩过的坑(附录一)》

    C++之那些年踩过的坑(附录一) 作者:刘俊延(Alinshans) 本系列文章针对我在写C++代码的过程中,尤其是做自己的项目时,踩过的各种坑.以此作为给自己的警惕. [版权声明]转载请注明原文来自 ...

  10. SpringMVC4+MyBatis+SQL Server2014 基于SqlSession实现读写分离(也可以实现主从分离)

    前言 上篇文章我觉的使用拦截器虽然方便快捷,但是在使用读串还是写串上你无法控制,我更希望我们像jdbc那样可以手动控制我使用读写串,那么这篇则在sqlsession的基础上实现读写分离, 这种方式则需 ...