ServletConfig和ServletContext
来源:互联网 发布:python 密度估计 编辑:程序博客网 时间:2024/06/18 04:51
一、二者存在的价值
1.ServletConfig主要用于加载servlet配置文件的初始化参数,主要服务于servlet。我们可以在servlet配置信息里面存放一些需要用到的信息,在web.xml文件中的servlet标签下的init-param标签下配置,每个name和value值都需要重新写一个init-param标签。配置如下所示:
<servlet> <servlet-name>...</servlet-name> <servlet-class>...</servlet-class> <init-param> //这个标签写在要设置的servlet内 <param-name>name</param-name> //属性名 <param-value>value</param-value> //属性值 </init-param> </servlet>
2.ServletContext主要用于操作全文的配置文件。我们知道一个Web应用里面可以有多个servlet,如果现在有一份数据需要传给所有的servlet使用,那么我们就可以使用ServletContext对象了。举个例子,像一些账号、密码,我们就可以放在该容器中,当忘记更改账号、密码时直接在web.xml文件中更改就行了,而不用更改源代码。下面是全局参数的设置方法:
<context-param> <param-name>user</param-name> <param-value>password</param-value></context-param>
二、两个对象的创建过程
1.ServletConfig的创建过程
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息
2.ServletContext的创建过程
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。ServletContext对象servlet上下文对象。该对象在一个web应用中只会创建一个对象,代表当前web应用的环境。在当前的web应用中所有servlet都共享一个ServletContext对象。
三、如何获取两个对象
1.获取ServletConfig对象
首先HttpServlet继承自GenericServlet类,而GenericServlet类又继承自Servlet接口,Servlet接口有方法getServletConfig() ,所以HttpServlet肯定有方法getServletConfig()。而GenericServlet类又实现了ServletConfig 接口,也就是他肯定实现了ServletConfig接口中的所有方法,在servlet文件中直接用this.getServletConfig()就可以获取ServletConfig对象,也可以直接在servlet中调getInitParameter(“name”)获取参数值。
2.获取ServletContext对象
开发者十分巧妙的将ServletContext对象放入在了该web应用里的每一个servlet中的ServletConfig对象里!在一个servlet被创建时,会调用它的构造方法,随后就会调用init()方法对它初始化,而在初始化的过程中,就把ServletContext对象放入到ServletConfig对象中了!那么这个servlet得到ServletContext对象对象就十分方便了,只需要用一句代码:
ServletContext servletContext = this.getServletContext();
四、两个对象的常用方法
1.ServletConfig常用的方法(config为ServletConfig对象)
- 根据名称获取参数值
String value=config.getInitParameter(String name)
- 得到所有参数
Enumeration enums=config.getInitParameterNames()
- 获取servlet上下文对象
ServletContext context=config.getServletContext()
- 获取当前Servlet在web.xml中配置的名字
String value=config.getServletName()
2.ServletContext常用的方法
- 通过文件后缀名得到媒体类型
ServletContext context = this.getServletContext();String mimeType =context.getMimeType(filename);
- 获取web应用的全局参数
ServletContext context = this.getServletContext(); Enumeration<String> enums = context.getInitParameterNames(); while(enums.hasMoreElements()){ String paramName = enums.nextElement(); String paramValue = context.getInitParameter(paramName);
- 域对象相关的方法(以request域为例)
servlet1写上:ServletContext context = this.getServletContext();context.setAttribute("id", 123);然后servlet2通过获得它ServletContext context = this.getServletContext();String id = context.getAttribute("id");
- 用于转发
ServletContext context = this.getServletContext();RequestDispatcher rd=context.getRequestDispatcher(String path)
- web应用加载资源文件方法
ServletContext context = this.getServletContext();String abosolutePate=context.getRealPath(String path); //得到资源的绝对路径ServletContext context = this.getServletContext();InputStream is=context.getResourceAsStream(String path) //得到资源的的一个输入流
- ServletContext和servletconfig区别
- ServletContext和ServletConfig分析
- ServletContext,ServletConfig和Sesion
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- (24)ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- servletConfig和servletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- Flume Error must not generate more than one output value per record field解决
- iOS 10中info.plist 文件 Privacy - Camera Usage Description等
- 移动图形芯片的故事
- SQL Server事务简单使用
- 文本滚动元素 marquee 研究报告
- ServletConfig和ServletContext
- linux下查询进程占用的内存方法总结
- 使用SVN问题总汇
- php中array_filter函数的总结
- jQuery获取textarea内容
- ViewStub的实现
- 数据结构实验之查找五:平方之哈希表
- 数据结构::矩阵(二)--稀疏矩阵
- 设计模式学习笔记--前端控制器模式