如何在web应用(如:Tomcat)启动时就加载相应的集合?如:省份列表人。 :)
[b]问题补充:[/b]
ServletContextListener
这个监听器还没用过,我想在应用启动的时候就把所有的省份列表放在一个Session,然后在页面取。用Servlet怎么做?谢谢!
[b]问题补充:[/b]
用下面代码实现,应用启动时该Servlet没有加载,只能显示的访问
public class ContentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
INewsCategoryService service = (INewsCategoryService) WebApplicationContextUtils
.getWebApplicationContext(
request.getSession().getServletContext()).getBean(
"newsCategoryService");
HttpSession session = request.getSession();
List<NewsCategory> list = new ArrayList<NewsCategory>();
try {
list = service.findNumber(10);
for (NewsCategory c : list) {
System.out.println(c.getCategoryName());
}
session.setAttribute("newsCategoryList", list);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is the description of my J2EE component
This is the display name of my J2EE component
ContentServlet
com.youcool.news.web.servlet.ContentServlet
1
[b]问题补充:[/b]
public void contextInitialized(ServletContextEvent arg0) {
。。。。。
}
在这个方法中如何取得Session,应用启动时没有请求!!