Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execute jsp code automatically at startup of server

 
Sabarish Venkat
Ranch Hand
Posts: 136
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to execute a jsp code automatically whenever the server is started. I tried with implementing the jsp file as a jar file and deployed in tomcat server but it doesn't start that program to run. Do i get any help regarding this. Thanks in advance.

Regards,
Sabi
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the point of that? The JSP is going to produce some HTML, but nobody requested that HTML. So where would you expect it to go?
 
Sabarish Venkat
Ranch Hand
Posts: 136
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, jsp doesn't take it to any other page . It will just execute some set of instructions in the code. Making you clear about the concept, In a case some set of things to be executed automatically at the time of deployment in server ( in other words When server is started this set of codes will be executed at back end) It doesn't take it to any page. I had made this jsp codes as a jar file and implemented in the library but when deployed in server this code doesn't affect.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then use servlet at load on startup or ServletContextListener
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am posting this for just to know[bad to implement it in your project] :

you can also execute a jsp on application startup like servlet
* not tested though...
 
Vicky Vijay
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could listener

<listener>
<listener-class>com.xyz.servlet.CustomizedListener</listener-class>
</listener>
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:I am posting this for just to know[bad to implement it in your project]

Yes, that really is bad practice, and you should not do this. A context listener is the right way to go about this; load-at-startup servlets, too, should not be used.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote: load-at-startup servlets, too, should not be used.

Tim, Please can you explain me why we should not use load-on-startup?
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me clarify: load-at-startup servlets should not be used for initialization. if their purpose is to warm up a servlet before use, that's fine, although few servlets should have such noticeable init work that it can't be suffered by even just the very first request.

But historically, load-at-startup servlets originate from a time when there were no context listeners, and they were used for init work - it's a hack to use an HTTP processor if there is no HTTP request, servlets just like JSPs.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the first - and biggest - mistake was the use of the fatal words "JSP code". JSPs should not contain code. Put the code in a Java class.
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sabi Swa wrote: I tried with implementing the jsp file as a jar file and deployed in tomcat server but it doesn't start that program to run.


How do you implement this? and what makes you think that it should or shouldn't work? Also, when you say it does not work, are you getting any error? Just saying it does not work, does not help much.

Also, I would be interested, what exactly you want to get done when server starts? There may be some better way to get it done.
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The better ways have already been suggested several times by now. Trying to get a JSP solution to work for this is a waste of time.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. The proper way to do this is with a context listener. Using a JSP for this is using a hammer to bang in a screw, and using a servlet is an old trick that is no longer necessary.

 
Sabarish Venkat
Ranch Hand
Posts: 136
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks to all As many of you suggest i go with the context listener.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic