• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Execute jsp code automatically at startup of server

 
Ranch Hand
Posts: 136
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then use servlet at load on startup or ServletContextListener
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could listener

<listener>
<listener-class>com.xyz.servlet.CustomizedListener</listener-class>
</listener>
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks to all As many of you suggest i go with the context listener.
 
reply
    Bookmark Topic Watch Topic
  • New Topic