I would like to load java class file that contain db related function. How can I load that java file while starting the tomcat server
Asked
Active
Viewed 1,639 times
-4
-
2have you tried using a singleton model for your db connection?? or static blocks?? what exactly is you issue with the thing you are trying to achieve?? – Abhishek Oct 05 '16 at 12:46
-
1Same way in which you refer to any other class file - you just refer to it and the JVM will load it when necessary. – Erwin Bolwidt Oct 05 '16 at 12:46
-
I am using Quartz scheduler to schedule jobs.Once if we stop server,quartz related data will be gone. If I start the server again i need to get the job details from db and supply to scheduler method of submitted job details – sathish kumar balu Oct 05 '16 at 12:48
-
1Totally reading between the lines here .. but i think you mean, How do you cange Quartz to use a JDBC store from In memory http://stackoverflow.com/questions/13633404/quartz-scheduler-what-is-the-diff-between-ram-and-jdbc-job-store – Kenneth Clark Oct 05 '16 at 12:54
-
1Set up Quartz to use a database. For example, using this help page here: http://teknosrc.com/how-setup-quartz-scheduler-server-with-mysql-database/ – Erwin Bolwidt Oct 05 '16 at 12:59
-
How to use JDBCTemplate in startupInitializer – sathish kumar balu Oct 05 '16 at 13:18
-
public class StartupInitializer { public void init() { } } @Erwin Bolwidt – sathish kumar balu Oct 05 '16 at 13:20
2 Answers
1
you can use servlet for that as below, define into web.xml
<servlet>
<servlet-name>YourServletName</servlet-name>
<servlet-class>com.abc.xyz.YourServletClassName</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
in YourServletClassName.java file you can write your code.
Hope it helps you.

PSabuwala
- 155
- 1
- 9
-
Did you ask about check authorization (spring-security) before your application load? – PSabuwala Oct 06 '16 at 08:44
-
Actually i didn't get, What is the link between your actual question and login authorization, Please explain. – PSabuwala Oct 06 '16 at 13:49
-
This is a pre-2001 solution. A generic solution for this would use a java.servlet.ServletContextListener. However it seems that the OP really needs to configure Quartz to do his dirty work instead. – Steve C Oct 07 '16 at 16:14
0
The answer by Psabuwala is correct but not complete.
Code that will run on startup should be placed in the init
method of the servlet.
Web.xml:
...
<servlet>
<servlet-name>mainServlet</servlet-name>
<servlet-class>example.com.MainServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
MainServlet.java:
public class MainServlet extends HttpServlet
{
public void init() throws ServletException
{
DataLoader dataLoader = new DataLoader();
dataLoader.load();
}
...
}

Yuriy N.
- 4,936
- 2
- 38
- 31