现在遇到一个问题,希望高手能指点迷津。在web应用启动时,执行一个class,该class读取DB中的某个资源表,把资源表设置到application中。不知道该如何实现?
难点一:web应用启动时如何执行某个类? 在web.xml中如何配置?该类是不是应该定义成static方式?
难点二:该类是不是应该配置到DB链接的后面?该怎么配
难点一:web应用启动时如何执行某个类? 在web.xml中如何配置?该类是不是应该定义成static方式?
难点二:该类是不是应该配置到DB链接的后面?该怎么配
具体listener怎么配置GOOGLE出来的都比我讲得好得多。如果数据不大,那么把读出的数据配置成static是可以的难点二:这个...我就不清楚了,自己试验最好,我想应该是配置到DB链接的后面的,如果用了Spring框架就好办了...
<web-app>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>InitServlet</servlet-class>
<load-on-startup>25</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>load-on-startup 是关键请参考下面这个文章,我收集的
http://www.java2000.net/viewthread.jsp?tid=315
public class InitServlet extends HttpServlet implements Servlet { public void init() throws ServletException {
super.init();
//DB处理方法
//复值 this.getServletContext().setAttribute(?,?);
//初始化标准编码
initSclistContext();
} public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
init();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
init();
}}