web.xml中配置启动时加载的servlet,load-on-starup
2014-03-07 13:02
381 查看
前阵子一直在搞前端结果搞得自己所学的java相关知识丢得一干二净。现在重新捡起来的时候,遇到N多问题,所以做笔记先。
这次项目中遇到我想搞清楚的小问题,就是项目加载后一些配置文件的读取,也就是启动的时候就将一些配置读到内存中以待使用。
使用servlet来初始化配置文件数据:
在servlet的配置当中,<load-on-startup>1</load-on-startup>的含义是:
标记容器是否在启动的时候就加载这个servlet。
当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
正数的值越小,启动该servlet的优先级越高。
web.xml:
<servlet>
<servlet-name>StartServlet</servlet-name>
<servlet-class>com.xtown.web.StartServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
servlet代码:
这次项目中遇到我想搞清楚的小问题,就是项目加载后一些配置文件的读取,也就是启动的时候就将一些配置读到内存中以待使用。
使用servlet来初始化配置文件数据:
在servlet的配置当中,<load-on-startup>1</load-on-startup>的含义是:
标记容器是否在启动的时候就加载这个servlet。
当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
正数的值越小,启动该servlet的优先级越高。
web.xml:
<servlet>
<servlet-name>StartServlet</servlet-name>
<servlet-class>com.xtown.web.StartServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
servlet代码:
public class StartServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { JDBServlet is = new JDBServlet(); is.ConfigFile = this.getClass().getClassLoader() .getResourceAsStream("conf.xml"); //将文件转换为输入流 is.start(); //设置从配置文件里读取过来的数据 String contextPath = getServletContext().getServletContextName(); if (contextPath.endsWith("/")) contextPath = contextPath.substring(0, contextPath.length() - 1); getServletContext().setAttribute("path", "/" + contextPath); //设置项目路径,用于设置绝对路径 } }
相关文章推荐
- J2EE中web.xml配置servlet的load-on-startup
- J2EE中web.xml配置servlet的load-on-startup
- 在web服务启动是就加载运行某个servlet --------load-on-startup
- web.xml中Servlet配置<load-on-startup>的作用
- web.xml配置中<load-on-startup>0</load-on-startup>的一个问题
- 在web.xml中配置Servlet,Servlet被WEB应用程序加载过程
- 新建servlet后再配置web.xml后tomcat不能启动,报错:Caused by: org.apache.catalina.LifecycleException: Failed to star
- 服务器启动时Webapp的web.xml中配置的加载顺序
- 使用@WebServlet配置loadOnStartup的时候不起作用的解决方法
- web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序 研究
- web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序 研究
- 使用@WebServlet配置loadOnStartup的时候不起作用的解决方法
- web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序 研究
- web.xml里配置load-on-startup的意思
- 01、配置tomcat的web.xml文件,使得方法在tomcat启动时被加载
- servlet通过web.xml配置在服务器启动时自动启动
- web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序研究
- 使用web.xml配置Web应用之初始化和预加载servlet和JSP页面
- web.xml中servlet load-on-startup标签的含义
- 谷歌验证码非web.xml非servlet拦截 使用配置文件自定义加载,防止多次点击(防js攻击)