Web启动加载资源的几种方式
2012-08-22 15:25
417 查看
1、struts1 plugin
例:
2、通过servlet来实现
例:
然后在web.xml配置一下:
其中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应用程序时调用此方法。
例:
//实现接口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"/> |
例:
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 { } } |
<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> |
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> |
相关文章推荐
- 访问web-inf下jsp资源的几种方式
- WEB项目启动加载的实现方式整理
- Android加载图片资源的几种方式
- javaWeb-web项目中加载资源文件路径的两种方式
- Sciter:加载资源的几种方式
- Struts2获取web资源的几种方式
- Struts2获取web资源的几种方式
- web项目启动时,自动执行代码的几种方式
- Android_webview加载资源方式
- web容器启动后自动执行程序的几种方式比较
- java Web 启动时自动执行代码的几种方式
- web 加载spring有几种方式
- 【Java】—— java Web 启动时自动执行代码的几种方式(总有些代码需要在虚拟机启动时执行)
- spring应用启动加载自定义类方法的几种方式
- java Web 启动时自动执行代码的几种方式
- Android: WebView加载网页的几种方式及网络异常处理
- WebView 加载资源的三种方式 一般使用第二种方式与H5交互
- Android的WebView加载网页的几种方式及网络异常处理
- 系统框架中加载资源文件几种方式