如果需要自启动,web.xml文件配置如下:
<servlet>
<servlet-name>t1</servlet-name>
<servlet-class>com.abc.test.T1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-name>t1</servlet-name>
<servlet-class>com.abc.test.T1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<load-on-startup>标记web容器是否在启动的时候就加载这个servlet,当值为0或者大于0时,表示web容器在应用启动时就加载这个servlet;
当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载;
正数的值越小,启动该servlet的优先级越高。
在写这个Servlet里,需要重写init()方法,其他如doGet(),doPost()是执行不到的。下面是一个最简单的例子:
package com.abc.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class T1 extends HttpServlet {
public T1() {
super();
}
public void init() throws ServletException {
System.out.println("test");
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class T1 extends HttpServlet {
public T1() {
super();
}
public void init() throws ServletException {
System.out.println("test");
}
}
重启web容器,在控制台可以看到"test"这么一行信息。
然后我们来修改我们的代码,让这个Servlet可以读取数据库的内容,并进行相关的处理。
我操作数据库用的是spring的HibernateTemplate和JdbcTemplate,访问数据库是以bean注入的方式,在action里注入service的实现,读取数据库数据,其实现在的关键就是得到这个service的实例化对象,
现在来看一下我们如何写这个servlet,代码如下:
package com.abc.test;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.abc.test.Service1;
public class T1 extends HttpServlet {
public T1() {
super();
}
public void init() throws ServletException {
ServletContext sc = getServletContext();
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
Service1 service = (Service1) ac.getBean("yourServiceId");
List list = service.getList();
// list就是从数据库取到的内容,你可以做相关的处理
}
}
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.abc.test.Service1;
public class T1 extends HttpServlet {
public T1() {
super();
}
public void init() throws ServletException {
ServletContext sc = getServletContext();
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
Service1 service = (Service1) ac.getBean("yourServiceId");
List list = service.getList();
// list就是从数据库取到的内容,你可以做相关的处理
}
}
补充说明:
若在init(ServletConfig config) 有参方法里处理要加 super.init(config); 不然 getServletContext()会报NullPointerException
原因:
重写了init(ServletConfig)方法,但重写的init方法内部没有调用super.init(config);就是这导致了错误!父类的 init(ServletConfig)有处理获取ServletContext对象的引用,在doGet/doPost/service方法方法中才能够通过 getServletContext()方法获取到SeverletContext对象!
public void init(ServletConfig config) throws ServletException {
super.init(config); //处理ServletContext对象的引用
ServletContext sc = getServletContext();
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
Service1 service = (Service1) ac.getBean("yourServiceId");
List list = service.getList();
// list就是从数据库取到的内容,你可以做相关的处理
}
ServletContext sc = getServletContext();
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
Service1 service = (Service1) ac.getBean("yourServiceId");
List list = service.getList();
// list就是从数据库取到的内容,你可以做相关的处理
}
yourServiceId 就是在spring配置文件里面配置的<bean id="yourServiceId">。很简单吧,然后你就可以做相关的操作了,比如要执行sql脚本,或者把从数据库里面取出来的数据放到application,或者static String里,等等。