Web启动加载资源的几种方式

1、struts1 plugin

例:

//实现接口org.apache.struts.action.PlugIn
public class WileyPlugin implements PlugIn {
public static final String PROPERTIES = "PROPERTIES";
public WileyPlugin() { }     //创建一个空构造函数
//实现接口方法
public void init(ActionServlet servlet, 
ApplicationConfig applicationConfig) 
throws javax.servlet.ServletException {
System.err.println("---->The Plugin is starting<----");
Properties properties = new Properties();
try {
  File file = new File("/WEB-INF/props.txt");
  FileInputStream fis =new FileInputStream(file);
  properties.load(fis);
  ServletContext context = servlet.getServletContext();
  context.setAttribute(PROPERTIES, properties);
  Properties rProperties = 
  (Properties)context.getAttribute (PROPERTIES);
  System.err.println("---->Prop: LOAD " +
   rProperties.getProperty("LOAD"));
}
catch (FileNotFoundException fnfe) {
  throw new ServletException(fnfe.getMessage());
}
catch (IOException ioe) {
  throw new ServletException(ioe.getMessage());
}
}
public void destroy() {

System.err.println("---->The Plugin is stopping<----");
}
}
//props文件内容:
LOAD=some load 

//配置struts-config.xml添加<plug-in/>元素
<plug-in className="wiley.WileyPlugin"/>

2、通过servlet来实现

例:

public class StaticClass extends javax.servlet.http.HttpServlet 
implements javax.servlet.Servlet  
{  
    private static final long serialVersionUID = 1L;  
    private static String title =" StaticClass ";  
      
    public void init(ServletConfig config)   
    {   
        System.out.println("init");  
        ........ 
    }   

    protected void doGet(HttpServletRequest request, 
	HttpServletResponse response) throws ServletException, 
	IOException   
    {  
    }     
      
    protected void doPost(HttpServletRequest request, 
	HttpServletResponse response) throws ServletException, 
	IOException   
    {  
    }        
} 

然后在web.xml配置一下:

<servlet> 
    <servlet-name> staticclass</servlet-name>   
    <servlet-class>   
        action.util.StaticClass  
    </servlet-class>   
    <load-on-startup> 1</load-on-startup>     
</servlet>  
 
<servlet-mapping>   
    <servlet-name> staticclass</servlet-name>  
    <url-pattern> /servlet/staticclass.jsp</url-pattern>  
</servlet-mapping> 

其中load-on-startup的含义是:容器启动时加载这个servlet的顺序,正常的取值范围是:负数,0-5,如果是负数或者没有这个标签,则容器在启动时不自动加载 这个servlet,如果是0-5,则按照顺序加载这个servlet,执行初始化方法init()。数字是0-5,加载顺序也是0-5。

3、不管是Struts1、Struts2还是其他的web层框架,它们目前基于的技术都是Servlet,只要根据web.xml找到那个启动类,我们就能通过覆盖该类的的init()方法来实现系统的初始化工作。可以写一个listener让它实现 ServletContextListener接口,在contextInitialized()方法中做想做的事情。将此listener配置到 web.xml中,Servlet容器如tomcat会在启动该web应用程序时调用此方法。

 public class InitListener implements ServletContextListener {  
 
    public void contextDestroyed(ServletContextEvent sce) {  
        System.out.println("web exit ... ");  
    }  
 
    public void contextInitialized(ServletContextEvent sce) {  
        System.out.println("web init ... ");  
        //系统的初始化工作  
        // ...  
    }  
 
} 
<?xml version="1.0" encoding="UTF-8"?>
<web-app> 
  <listener> 
    <listener-class>fangwei.listener.InitListener</listener-class>
  </listener>
  <filter> 
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher<
	/filter-class> 
  </filter> 
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern> 
  </filter-mapping>
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值