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 ...
随机推荐
- springmvc的ModelAttribute注解
先看一个没有使用@ModelAttribute的Controller方法. @RequestMapping("/save") public String save(User use ...
- O(logN)中logN的底数
转载:http://blog.csdn.net/jdbc/article/details/42173751 问题: 无论是计算机算法概论.还是数据结构书中, 关于算法的时间复杂度很多都用包含O(log ...
- Android fragment切换后onresume时报 Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim'
动态加载fragment以后,调用了remove方法移除Fragment,在返回来的时候报 Attempt to write to field 'int android.support.v4.app. ...
- MySql中的varchar长度究竟是字节还是字符
今天在设计表的时候,遇到个小问题,由于不知道未来将要存储的数据有多长(数据是通过第三方http接口提供的,根据sample显示,数据大概是如下:) 也就是6个字符. 我在设计表的时候,有点犹豫,本来准 ...
- 我做的python常用的小技巧
在python编码过程中,总会遇到各种各样的小问题,我想着记录下来,以备查用,总结过去,是为了更好的思考与进步. 一. 去除变量中(标题中)多余的字符 数据处理过程中,遇到这样的情况: y=['月份' ...
- uvalive 3213 Ancient Cipher
https://vjudge.net/problem/UVALive-3213 题意: 输入两个字符串,问是否可以由第一个字符串的每个字符一一映射得到第二个字符串,字符是可以随意移动的. 思路: 统计 ...
- Ubuntu下发射wifi
iphone要连接的话,mode选Ad-hoc, wifi密码要用WEP 40/128-bit key模式
- 原生js中实现全选和反选功能
<!DOCTYPE html> <html> <head lang="en"> <meta char ...
- Lintcode375 Clone Binary Tree solution 题解
[题目描述] For the given binary tree, return a deep copy of it. 深度复制一个二叉树,给定一个二叉树,返回一个他的克隆品. [题目链接] www. ...
- 遗传算法详解(LINGO及MatlabGA工具箱求解实现)
遗传算法 1.前言 遗传算法是一种基于生物界自然群体遗传进化机制的自适应全局优化概率搜索算法.它与传统算法不同,不依赖梯度信息,而是通过模拟自然进化过程来搜索最优解. 例子:兔子的遗传进化 有人说,现 ...