aspose file tools*
The moose likes JSP and the fly likes Execute jsp code automatically at startup of server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Execute jsp code automatically at startup of server" Watch "Execute jsp code automatically at startup of server" New topic
Author

Execute jsp code automatically at startup of server

Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

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
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

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

Joined: Jan 18, 2012
Posts: 136

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

Joined: Jan 28, 2008
Posts: 5575

then use servlet at load on startup or ServletContextListener
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 23, 2008
Posts: 125
You could listener

<listener>
<listener-class>com.xyz.servlet.CustomizedListener</listener-class>
</listener>
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jan 28, 2008
Posts: 5575

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jun 25, 2001
Posts: 16308
    
  22

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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.



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sabarish Venkat
Ranch Hand

Joined: Jan 18, 2012
Posts: 136

Hi,
Thanks to all As many of you suggest i go with the context listener.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute jsp code automatically at startup of server