File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Running Thread in Servlet container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running Thread in Servlet container" Watch "Running Thread in Servlet container" New topic
Author

Running Thread in Servlet container

Fabio Fonseca
Greenhorn

Joined: Jan 19, 2006
Posts: 29
Hello all!

I don't even know if my question is in the correct board, but here it goes.

For the past 5 months my duty in my company is to take care of our web site, which is written in java, using frameworks such as hibernate, log4j and some others. For the time being I have added classes, upgraded stuff, played with hibernate, etc. Anyway, this site was running smoothly already when I was assigned to take care of it.

But now I need to code a simple project from scratch and questions are appearing everywhere.

My first job is to get some external files using FTP and then parse it using JSLT and jsp. How do I accomplish that?

I've written some "normal" (javabean) thread code that implements this feature. But how do I add it to my webapp? I mean, how can I make my webapp aware of this javabean and run it when I want? I know that servlet code has no main() method, but how can I call this class whenever I want? do I need to write a class that starts together with webapp and tell the FTP class to start itself? If yes, how do I make that?

here it goes the prototype code so far:


thanks in advance!
[ June 06, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

I'm not sure exactly what you're trying to do, but if you are trying to trigger some code when the web app starts, look into using a context listener.

It is not appropriate to try and trigger one-time startup code from a servlet or a JSP which only executes as the result of a response from a client.
[ June 01, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
You can write a servlet which you will access using a browser. HTML page for control of functionality should include buttons (links) like run, stop, check and so on for your service. Servlet uses method doGet instead of main. A request can have parameters you normally get in main as well. doGet will be called one you click above command links.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

As Bear put it, its really not clear what your requirements are. Specifically

1. Who initaites the ftp process? A request from a client or application startup?

2. If its through a client request, kicking off a thread is not safe (you may end up with more threads that you can cope with). Also thread creation would make the process asynchronous.
If its on application start, which I guess it is, use the listener as Bear suggested. You'll have to manage the downloaded file list which should be open for client preview through a request process.

3. Where exactly does the jsp/jstl parse come into the picture?

ram.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
While we wait to hear more details ... you get extra points for I've written some "normal" (javabean) thread code .... Code you develop outside your web app can't possibly have any dependencies on web stuff, which makes it all the cleaner and easier to test and reuse and all sorts of good things. With any luck you'll find integrating it now pretty simple!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Originally posted by Stan James:
you get extra points for I've written some "normal" (javabean) thread code ....


+1
Fabio Fonseca
Greenhorn

Joined: Jan 19, 2006
Posts: 29
Originally posted by ramprasad madathil:
As Bear put it, its really not clear what your requirements are. Specifically

1. Who initaites the ftp process? A request from a client or application startup?

2. If its through a client request, kicking off a thread is not safe (you may end up with more threads that you can cope with). Also thread creation would make the process asynchronous.
If its on application start, which I guess it is, use the listener as Bear suggested. You'll have to manage the downloaded file list which should be open for client preview through a request process.

3. Where exactly does the jsp/jstl parse come into the picture?

ram.


Hello guys! It's me again. Thanks for the extra points. If I counted 'em correct, my HP is now HP += 2. Am I correct?

About the questions, it took me a while to identify what I was really doing - and needing. After some research, I've discovered that I should use the <listener> tags in my web.xml in order to call the classes that will initiate my web app. So, making things clearer and actually replying the questions:

1. The application startup should initiate the process, grab 5 or so XML files in a FTP server and save it in the root directory of our web app.

2. Ah! I need this thread because everyday those 5 or so XML files are recreated with new information - TV Schedules, for the curious. But no user whatsoever will ever call this thread, only the web app.

3. I will use the JSTL�s XML tags to load, parse and transform (XSLT) the information in those files and output it in a XHTML format, using JSP.


You're gonna hear from me very soon, since all sort of errors are popping from everywhere in my app! The JSTL XML tags does not works, the listener doesn't listen.. but the Jakarta's XTAGS are doing pretty fine. Parsing and displaying of the XML info is fine! Just hope Jakarta doesn't drop support to them until I finish this work.
 
Consider Paul's rocket mass heater.
 
subject: Running Thread in Servlet container
 
Similar Threads
isAlive returns false, but thread start throws java.lang.IllegalThreadStateException
Updation of ProgressBar??
Date Difference
Updation of ProgressBar??
Caching Issues with Hibnernate