2015-11-15 69 views
1

我正在創建一個由tomcat託管的Vaadin web應用程序(可部署的war文件)。在從客戶端獲取請求之前做一些事情,tomcat war

有沒有辦法做一些事情(比如說創建一個對象)在戰爭文件的部署,在初始化之前或得到來自客戶端的請求之前?

莫非通過覆蓋

void init() 

方法來實現?我沒有一個清晰的想法。我對此很陌生。先謝謝了。

回答

3

當服務器啓動或更精確地,當servlet容器啓動後,它將部署所有Web應用程序,加載它們,然後爲每個應用程序創建一個應用程序上下文並將其存儲在其內存中。我提到了上述情況,以便您對問題的解決方案有更好的理解。

現在來談談你的問題,你可以做的是創建一個類並命名它,然後執行ServletContextListener接口。基本上有兩種方法具有以下簽名。

  • 公共無效的contextInitialized(ServletContextEvent事件)
  • 公共無效contextDestroyed(ServletContextEvent事件)

現在在contextInitialized方法,你可以做任何你想要的,如創建對象或東西,因爲這是當你的ServletContext被初始化時被調用的方法。

在web.xml的地方如下

<listener> 
    <listener-class> 
     your fully qualified class name that which will implement the ServletContextListener 
    </listener-class> 
</listener> 

我希望這回答你的問題的映射。樂於幫助。

2

可以添加另一個類可以由web.xml負載上啓動時,指定自動加載= 1:

例如:

<web-app> 
    <servlet> 
     <servlet-name>MyLoader</servlet-name> 
     <servlet-class>com.xxx.MyLoader</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>MyLoader</servlet-name> 
     <url-pattern>/load</url-pattern> 
    </servlet-mapping> 
</web-app> 
2

以上答案都工作,但如果你想另一種選擇,你可以覆蓋默認Vaadin的servlet的init方法。

public class MyServlet extends com.vaadin.server.VaadinServlet { 
    @Override 
    public void init(javax.servlet.ServletConfig servletConfig) throws ServletException { 
     super.init(servletConfig); 
     // do extra work here! 
    } 
} 

請注意,您需要將您的web.xml或註釋的UI類配置與新SERVET,例如初始化你vaadin應用改變你的'web.xml中

<servlet> 
    <servlet-name>YourAppName</servlet-name> 
    <servlet-class>path.to.MyServlet</servlet-class> 
</servlet> 
+0

嗨,你可以檢查我的有關Vaadin的問題在:http://stackoverflow.com/questions/33783130/vaadin-widget-set-is-not-getting-loaded。非常感謝... :-) –

相關問題