當前位置:首頁 » 網頁前端 » web啟動後執行
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

web啟動後執行

發布時間: 2022-08-20 03:25:44

『壹』 如何在一個web系統啟動之後,馬上執行一個方法!怎麼配置

web.xml配置
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
在servlet的Init里定義你要開始載入的方法 配置文件的意思http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html

『貳』 web伺服器啟動時執行一個動作,代碼怎麼寫

不知道你要執行什麼動作,也不講清楚

『叄』 spring定時器執行不了為什麼是WEB程序啟動後自動執行

我記得要加 task:annotation-driven 標簽的

『肆』 java Web 啟動時自動執行代碼的幾種方式

Web容器啟動後執行代碼的幾種方式
其執行順序為:

4===>5===>1===>2===>3
即指定init-method的Bean開始執行
接著實現spring的Bean後置處理器開始執行
然後是Servlet的監聽器執行
再接下來是Servlet的過濾器執行
最後才是Servlet執行

1、實現Servlet監聽器介面ServletContextListener

[java] view plain
public class InitListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent context) {

}

@Override
public void contextInitialized(ServletContextEvent context) {
// 上下文初始化執行
System.out.println("================>[ServletContextListener]自動載入啟動開始.");
SpringUtil.getInstance().setContext(
<span style="white-space:pre"> </span>WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext())
<span style="white-space:pre"> </span>);
}

}
然後在web.xml文件配置該監聽器

[html] view plain
<listener>
<listener-class>com.test.init.InitListener</listener-class>
</listener>

2、實現Servlet的過濾器Filter

[html] view plain
public class InitFilter implements Filter {

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException,
ServletException {

}

@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("================>[Filter]自動載入啟動開始.");
// 讀取Spring容器中的Bean[此時Bean已載入,可以使用]
//寫啟動需要執行的代碼
System.out.println("================>[Filter]自動載入啟動結束.");
}

}
然後在web.xml文件配置過濾器即可

[html] view plain
<filter>
<filter-name>InitFilter</filter-name>
<filter-class>com.test.init.InitFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>InitFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>

3、編寫一個Servlet,在web.xml裡面配置容器啟動後執行即可

[html] view plain
public class InitServlet extends HttpServlet {

/**
*/
private static final long serialVersionUID = 1L;

@Override
public void init(ServletConfig config) {
try {
super.init();
} catch (ServletException e) {
e.printStackTrace();
}
System.out.println("================>[Servlet]自動載入啟動開始.");
// 讀取Spring容器中的Bean[此時Bean已載入,可以使用]
//執行想要的代碼
System.out.println("================>[Servlet]自動載入啟動結束.");
}
}

然後在web.xml文件配置該Servlet的啟動方式為:容器啟動後執行
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.test.init.InitServlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>test</param-value>
</init-param>
<!-- 此處指定載入順序為2,表明還有優先順序更高的Servlet要先執行 -->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
關於啟動後執行,由load-on-startup指定:
(1)當值為0或者大於0時,表示容器在應用啟動時就載入這個servlet。值越小,啟動優先順序越高;
(2)當是一個負數時或者沒有指定時,表示該servlet被調用時才載入。

4、如果你使用Spring IOC作為Bean管理容器,那麼可以指定init-method其中init-method表示bean載入成功後,立即執行某個方法。配置如下:start為要執行的方法名稱

[html] view plain
<!-- service -->
<bean id="shopService" class="com.test.teach.service.ShopService" <span style="color:#33ffff;">init-method="start"</span>>
<property name="shopDao" ref="shopDao" />
</bean>

『伍』 我用spring的定時器隔半個小時去扒數據,但是我想web啟動就立即執行,然後後隔半個小時執行,不知道怎麼配

一起動的時候就調用,這個你可以寫個監聽,第一次調用在監聽里調用。
以後每隔半小時調用可以使用定時器。

我們定時器是用的quartz.先引入這個的Jar包。

在application-context.xml中,進行相關的配置。

定義一個任務,用來調用方法執行指定的任務。
<bean id="hourTask"
class="org.springframework.scheling.quartz.">
<property name="targetObject" ref="方法所在的類的bean的ID"/>
<property name="targetMethod" value="指定的方法"/>
<property name="concurrent" value="false"/>
</bean>

然後定義一個表達式,定義什麼時候執行調用指定的任務。
<bean id="cronReportTriggerHour" class="org.springframework.scheling.quartz.CronTriggerBean">
<property name="jobDetail" ref="hourTask"/>
<property name="cronExpression" value="0 0/30 0 * * ?"/>
</bean>

最後調用上面的定時器
<bean id="schelerFactory" class="org.springframework.scheling.quartz.SchelerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronReportTriggerHour"/>
</list>
</property>
</bean>
要先在相關的配置文件中配置類的bean,targetObject指向這個類,targetMethod指向執行任務的方法。
希望可以幫到你。

『陸』 如何讓spring mvc web應用啟動時就執行特定處理

一、ApplicationContextAware介面
package org.springframework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.context.ApplicationContext;

public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext var1) throws BeansException;
}

二、ServletContextAware 介面
package org.springframework.web.context;

import javax.servlet.ServletContext;
import org.springframework.beans.factory.Aware;

public interface ServletContextAware extends Aware {
void setServletContext(ServletContext var1);
}

三、InitializingBean 介面
package org.springframework.beans.factory;

public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}

四、ApplicationListener<ApplicationEvent> 介面
package org.springframework.context;

import java.util.EventListener;
import org.springframework.context.ApplicationEvent;

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E var1);
}

示常式序:
package test.web.listener;

import org.apache.logging.log4j.*;
import org.springframework.beans.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import javax.servlet.ServletContext;

@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
InitializingBean, ApplicationListener<ContextRefreshedEvent> {

protected Logger logger = LogManager.getLogger();

@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
logger.info("1 => StartupListener.setApplicationContext");
}

@Override
public void setServletContext(ServletContext context) {
logger.info("2 => StartupListener.setServletContext");
}

@Override
public void afterPropertiesSet() throws Exception {
logger.info("3 => StartupListener.afterPropertiesSet");
}

@Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
logger.info("4.1 => MyApplicationListener.onApplicationEvent");
if (evt.getApplicationContext().getParent() == null) {
logger.info("4.2 => MyApplicationListener.onApplicationEvent");
}
}

}

運行時,輸出的順序如下:

1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent

注意:onApplicationEvent方法會觸發多次,初始化這種事情,越早越好,建議在setApplicationContext方法中處理。

『柒』 做java web項目,在tomcat啟動後,執行某個功能,第一次執行會報錯。以後不會。求解答~~

castexception應該是你強制轉型出現的錯誤吧,上面指出了出錯位置是在indexaction的57行,你可以試試扔出異常,或在try裡面處理

『捌』 如何讓spring mvc web應用啟動時就執行特定處理

第一步:在spring的配置文件中增加一個bean配置,如:

<bean class="com.test.InitBean"></bean>
第二步:添加對應的bean類,實現org.springframework.beans.factory.InitializingBean介面,該介面只有一個afterPropertiesSet方法,應用啟動時spring載入完成後,即會調用這個方法,在方法中調用你需要的操作即可,如:
@Override
public void afterPropertiesSet() throws Exception {
//TODO 你要做的操作

『玖』 Java中如何讓web伺服器啟動的時候自動運行web程序中某個類的某個

1、首先讓需要自動運行的類繼承javax.servlet.http.HttpServlet

2、把需要自動運行的類中寫一個init方法。(servlet應用程序啟動的入口就是init方法)

publicvoidinit(){
System.out.println("這樣在web容器啟動的時候,就會執行這句話了!");
}

3、在web.xml中新建一個servlet,如下:

<servlet>
<servlet-name>GenerateData</servlet-name>
<servlet-class>com.yq.javaSCADA.business.impl.GenerateData</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

4、啟動的web伺服器,tomcat,weblogic,jboss,就會自動運行類中的init方法了。

『拾』 java web項目啟動後執行某個方法

filter 攔截器是能這么做的,但是打字太費勁了;
和你說個簡單的場景,web項目啟動後,會跳轉一個index.jsp吧,你用ajax 在jsp中調用一下;如果在index.jsp就要使用此方法返回的數據,讓項目跳轉到index0.jsp ajax跳轉到index.jsp 就可以了;
說個思路,此方法也就取個巧