servlet context 和 servlet config
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的更多相关文章
- 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 ...
- NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
今天调试SSM框架项目后台JSOn接口,报出来一个让人迷惑的错误:NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config 上网查了一下别人的博 ...
- springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
springMVC开发web的时候,报错:java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 原因:未引入jstl ...
- 报错:严重: 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 [ ...
- Spring的servlet context和application context
Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml def ...
- 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. ...
- 报错!!!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 = ...
- 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 ...
- 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 ...
随机推荐
- CSS3学习笔记(4)-CSS3函数
p{ font-size: 15px; text-indent: 2em; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid ...
- 3.Maven坐标和依赖
1.1 何为Maven坐标 正如之前所说的,Maven的一大功能就是管理项目依赖.为了能自动化地解析任何一个Java构件,Maven就必须将它们唯一标识,这就依赖管理的底层基础——坐标. 1.2 坐标 ...
- C#中string和byte[]相互转换问题解决
本来想讲string转换为byte数组,通过在VS上打 ‘str. “来找,结果半天没发现跳出来的函数中有想要的,哭瞎 /(ㄒoㄒ)/~~ 这回将两种情况都记下来了.... string ---> ...
- Centos7多网卡绑定操作,通过nmcli命令操作。
运行 ip link 命令查看系统中可用的接口1.创建bond网卡nmcli con add type team con-name team0 ifname team0 config '{" ...
- 关于String类和String[]数组的获取长度方法细节
一.在Java中,以下代码段有错误的是第( )行 public static void main(String[] args) { String name = "小新"; ...
- 微信公众号、H5、APP三者各有什么优势?
昨天给大家分享了一个现在很热的H5,众所周知,当下H5手机网站.微信公众号.APP这三种载体都越来越火了,而且三者都有各自的一些优势和劣势. HTML5(H5) H5之所以能引发如此广泛的效应,根本在 ...
- [ext4]01 磁盘布局 - block分析
ext4文件系统最基本的分配单元是"block"(块). block是由一组连续的sectors来组成,其大小介于1k~4K之间,当然不可能是任意值,只能是2的整数次幂个secto ...
- Angular回到顶部按钮指令
之前的分页代码指令化在线下测试没有问题,到服务器上就不运行了,所以那个先放一放. 今天来把"回到顶部"按钮指令化.首先是页面html: <!--回弹按钮--> < ...
- AngularJS的相关应用
一.[AngularJS常用指令] 1.ng-app:声明Angular所管辖的区域.一般写在body或html上,原则上一个页面只有一个: <body ng- ...
- tpcc-mysql的使用
1.tpcc-mysql的业务逻辑及其相关的几个表作用: New-Order:新订单,一次完整的订单事务,几乎涉及到全部表 Payment:支付,主要对应 orders.histo ...