servlet生命周期
1.如果是第一次访问servlet,则创建servlet实例
2.自动调用init(ServletConfig config)方法
3.调用service()方法
4.终止阶段调用destory()方法
Servlet为接口,GenericServlet是一个实现Servlet接口的抽象类,HttpServlet是GenericServlet类的子类
GenericServlet类
该类覆盖了init(ServletConfig config)方法,同时新加了一个init()方法(此方法是无参的),并且前者调用了后者。
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
public void init() throws ServletException {
// NOOP by default
}
init(ServletConfig confg)方法中this.config = config将该类的成员变量赋值,同时使得该类中的getInitParameter()方法得以使用this.config来调用ServletConfig类的getInitParameter()方法来取得初始值。所以在GenericServlet类的子类中如果要重写inti(ServletConfig config)方法则应该调用super.init(config),使之完成this.config的赋值。
HttpServlet类
当实现该类时如果要重写init(ServletConfig config)方法则应该要调用super.init(config),但是由于其父类中有init()方法,并且父类中的init(ServletConfig)调用了init()方法,所以在HttpServlet类中直接重写init()方法可以有效避免重写init(ServlerConfig config)方法时没有调用super.init(config)造成的空指针异常。
web.xml初始化参数后得到的值一直为null
造成这种情况的原因有很多,下面这种不是很明白原因
在servlet3.0中新增了注解功能,并且在创建servlet时会默认使用注解,此时在web.xml中是没有<servlet></servlet>标签的,如果此时在web.xml中加入标签例如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWebDemo2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletDemo2</servlet-name>
<servlet-class>psl.servlet.study.ServletDemo2</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>happy</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo2</servlet-name>
<url-pattern>/ServletDemo2</url-pattern>
</servlet-mapping>
</web-app>
servlet部分代码
/*
webServlet注解就相当于<servlet-mapping>标签,注意此处的“/ServletDemo2”与xml中<url-pattern>是
一样的功能也一样
*/
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
private ServletConfig config;
//此处省略若干代码
public void init()throws ServletException{
config = getServletConfig();
String myname=config.getInitParameter("name");
}
//此处省略若干代码
}
由于@WebServlet中的参数与web.xml中<url-pattern>标签一样,则会报错:Servlet Tomcat v9.0 Server at localhost failed to start。如果将web.xml中的<servlet-mapping>标签整个删掉变成如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWebDemo2</display-name>
<welcome-file-list>
<!--
此处省略若干代码
-->
</welcome-file-list>
<servlet>
<servlet-name>ServletDemo2</servlet-name>
<servlet-class>psl.servlet.study.ServletDemo2</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>happy</param-value>
</init-param>
</servlet>
</web-app>
则init()方法中的myname始终为null,但是config不为null,重写init(ServletConfig config)方法所得结果一样。如果在web.xml中将ServletDemo2配置一个不同于@WebServlet中的url,例如在xml中加入如下代码:
<servlet-mapping>
<servlet-name>ServletDemo2</servlet-name>
<url-pattern>/dsfa</url-pattern>
</servlet-mapping>
则myname值不为null。
~~~~~~~~~~~~~~~~~~知识水平有限,如有错误还望指正~~~~~~~~~~~~~~~~~~~~~~