Servlet init()
有时候希望在servlet首次载入时,执行复杂的初始化任务,但并不想每个请求都重复这些任务的时候,用init()方法他在servlet初次创建时被调用,之后处理每个用户的请求时,则不在调用这个方法。因此,他主要用于一次性的初始化,和applet的init方法相同。
由于servlet初始化参数的使用非常依赖于部署描述文件(web.xml),该文件可存放servlet所需要的起始参数以及web应用程序的结构数据。当servlet容器读取web.xml文件内容后。可以将这些起始参数封装成一个对象并在调用init方法时传递个servlet,这个对象就是ServletConfig对象所以我们可以在Servlet内覆写init方法,并通过ServletCongig对象来取得某些初始参数。
以init参数的名称为参数,调用ServletConfig的getInitParameter方法。返回值就是init参数的值。
init方法是在Servlet实例化之后执行的,并且只执行一次。
一.先说init(ServletConfig)中参数ServletConfig,代表的是配置信息。即在web.xml中配置的信息,比如:
<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
lt;/servlet>
在程序中可以用this.getServletConfig()方法得到ServletConfig的实例,然后用ServletConfig的相应方法 可以得到ServletConfig的名字(getServletName)和配置参数的名字(getInitParameter("name"))或者 名字枚举(getInitParameterNames()),并且通过参数名字得到相应的参数值。具体方法参见API。
二.再说说init方法,从源码中我们不难发现:Servlet接口里面只有init(ServletConfig),这是供tomcat调用的。GenericServlet类里面有成员变量ServletConfig,init(ServletConfig)方法和init()方法:
private transient ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
现在一切都很明了了,当容器(tomcat)帮忙调用init(ServletConfig config)并且给传过来一个参数config,这个方法把参数对象的引用指向类的成员变量this.config,并且调用类的 this.init()方法。如果我们在写Servlet类时只要重写init(ServletConfig config)就可以了,但是init()不就成了多余的了吗?实际上init()方法是为了防止程序员在写Servlet类重写 init(ServletConfig config)时忘记写super.init(ServletConfig config),这样就容易造成出现空指针异常。而这就要求我们最好不要重写init(ServletConfig config)。
Servlet init()的更多相关文章
- 报javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception异常 的解决方案
后台错误信息如下: javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception org.a ...
- HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception
HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception
- Spring MVC 使用问题与解决--HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
1.HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 解决 使用jre1.7 Spring4.3 2.spr ...
- javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception
type Exception report message Servlet.init() for servlet springmvc threw exception description The s ...
- 项目中访问controller报错:HTTP Status 500 - Servlet.init() for servlet spring threw exception
直接访问controller路径http://localhost:8080/index报错: HTTP Status 500 - Servlet.init() for servlet spring t ...
- 深圳宝安图书馆官网错误 HTTP Status 500 - Servlet.init() for servlet spring threw exception
停留了一段时间没有动 打开https://www.balib.cn/balib/category/152 *********************************************** ...
- Servlet.init() for servlet [springmvc] threw exception
项目还没开始做,就碰到那么多问题.. 报错一:/oa/news/%E6%A0%8F%E7%9B%AE%E7%AE%A1%E7%90%86.jsp 1.一开始是jsp的页面名称为中文,改了 2.接着仍然 ...
- Servlet[JAX-RS Servlet]的Servlet.init()引发异常
代码环境 Eclipse2017 : 问题出现: 在测试Hello servlet时发生 org.apache.catalina.core.ApplicationContext log严重: Serv ...
- JavaWeb核心编程之(三.3)Servlet Init 配置
Servlet初始化 可以传入一些参数 通过 <init-param>来配置 新建 servletinit项目 新建包 com.xiaoan.test->new Class(Test ...
随机推荐
- wamp的mysql设置用户名和密码
wamp下修改mysql root用户的登录密码 感谢作者:http://www.3lian.com/edu/2014/02-25/131010.html 1.安装好wam ...
- api-gateway实践(01)服务网关 - 原型功能
一.服务注册 1.增加组:LsqGrpA 2.增加版本:LsqVerA 3.增加api:LsqApiA 3.1.基本信息 3.2.前端定义 3.3.后端定义 二.服务上线和服务授权 1.服务上线 2. ...
- java stream 原理
java stream 原理 需求 从"Apple" "Bug" "ABC" "Dog"中选出以A开头的名字,然后从中选 ...
- 前端之CSS内容
一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). 二.CSS语法 1 ...
- 如何设置eclipse 右键new的菜单
如何设置eclipse 右键new的菜单 在使用eclipse进行开发的时候,开发人员一般使用File-new来创建项目或文件,但常常发现,默认右键new选项里很多选项极少会用到,而一些常用的选项又没 ...
- centos6.5时间相关
时间同步 service ntpdate start 开启网络时间同步
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- 已操作文件的方式,新建一个用户alex
- 云服务器ECS优惠券 阿里云 ecs 5折优惠码 阿里云5折优惠码 阿里云5折推荐码 阿里云优惠码 阿里云的5折优惠券 阿里云服务器购买优惠码 服务器购买优惠码
阿里云代金券 | 阿里云优惠券云服务器ECS,就是阿里云服务器,大家一定要清楚.云服务器ECS优惠券官方领取优惠页面:https://promotion.aliyun.com/ntms/act/amb ...
- git出现错误原因解释
原因,在pull下拉代码或者push之前,你本地还有代码没有进行commit. 引起下面的错误. 建议commit后先pull再看看有没有冲突在进行push. git.exe push --pro ...