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> |