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 The ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "The ServletContext" Watch "The ServletContext" New topic
Author

The ServletContext

alberto gonzalez
Greenhorn

Joined: Sep 05, 2005
Posts: 14
Hi There,

First I just want to mention that I am new to the world of servlets.
I am building a Web App and I am using the ServletContext a lot. I just wanted to know if there are any problems when doing this, does it slow the app if you store a lot of objects in the ServletContext or if this Objects are too big (although I am guessing you just store a reference to them). Is it better to create static variables?. I want to know if I am doing things the way they are supposed to be done or basically get some tips on what the best way to do things is.

Thanks
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
does it slow the app if you store a lot of objects in the ServletContext?

Yes it retards performance a bit. Just store those objects at servlet context which you really need application wide. Actually servlet context means application context. It is not at all considered best practice if you store objects at servlet context without any rational reason/desire.


or if this Objects are too big (although I am guessing you just store a reference to them).

Quite right. you win.
Yes its the same for context whether the object is big or not. because, as you already know that it stores the reference not the object. But, surely, storing big un-necessary objects at application context results in memory shortage and other problems.

Is it better to create static variables?.
No. But you can make if you really want to. I avoid static, and it doesn't mean i don't make any.
alberto gonzalez
Greenhorn

Joined: Sep 05, 2005
Posts: 14
Thanks for your help,

[I like the confusing comment!]

Since you are here and seem very knowledgeable in the subject can you tell me if what I am developing is a mad idea or it will be as reliable as if I use a Standalone Java app.

I need to write a piece of code that will scan a folder constantly for new files. The way I have designed this is as follows:
When the user calls the main Servlet this servlet will create a new thread that will have an infinite loop that will keep on checking the folder for files (I will be using the thread.sleep so that the loop is not running constantly). Once that thread is created any other call to the servlet will redirect the user to a JSP that will give information about how the process is going, number of files processed etc..

The things that this code has to support are:

- Real time response. A file needs to be processed as soon as the file reaches the folder (and then it will be deleted)
- The code needs to be running 24x7 365 days a year. It has to be as efficient as possible
- It would be a plus if you could have more than one process running at a time so that for those times of the day where the folder is receiving lots of files may be you could run more than one process at a time (basically create another thread running another loop).


Do you think that to run this using a servlet is a crazy idea?
The reason why I have chosen to do it with a web app is because it will provide an easy way of accessing the app from any computer without installing any software(a part from the browser) an easy user interface to check progress at any time and change settings while the app runs etc. I can use the security that features of a web app


If you can think of any reason why this may not be a good idea I will appreciate your comments
Rajendra Pachouri
Greenhorn

Joined: Sep 16, 2005
Posts: 10
Originally posted by alberto gonzalez:
Hi There,

First I just want to mention that I am new to the world of servlets.
I am building a Web App and I am using the ServletContext a lot. I just wanted to know if there are any problems when doing this, does it slow the app if you store a lot of objects in the ServletContext or if this Objects are too big (although I am guessing you just store a reference to them). Is it better to create static variables?. I want to know if I am doing things the way they are supposed to be done or basically get some tips on what the best way to do things is.

Thanks


123
alberto gonzalez
Greenhorn

Joined: Sep 05, 2005
Posts: 14
??
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by alberto gonzalez:
Do you think that to run this using a servlet is a crazy idea?
The reason why I have chosen to do it with a web app is because it will provide an easy way of accessing the app from any computer without installing any software(a part from the browser) an easy user interface to check progress at any time and change settings while the app runs etc. I can use the security that features of a web app


You are using servlet just because you need a simple interface and you dont need your program to be installed and all. No other reasons behind it. Actually servlet is a web technology, you have no reasons to run your app on web. So I dont think its a good idea. Moreover, you will find it less effcient because of performance lack.

Nevertheless, you can make it a client/server application, using socket programming or RMI, and put it on a server and then make it accessable to other machines via java web start. How it sounds?

And sorry for the late reply. Its just because of a weekend, indeed.
[ September 19, 2005: Message edited by: Adeel Ansari ]
alberto gonzalez
Greenhorn

Joined: Sep 05, 2005
Posts: 14
You don't have to apologize, I am very grateful that you respond my questions.

I get your point about the Servlet being a web technology, but the thing is that I am familiar with web technologies, so it makes me more efficient when coding. The machine where this code is going to run comes with TOMCAT already installed and this will be the only app running in that copy of TOMCAT. I have tested the most demanding part of the application and the performance will not be an issue.

I guess my question is, if the Tomcat server will cope with it as well as a JVM on its own if I was to write it as a Standalone application? This application will only have a very few people opening a JSP to monitor how the internal process is doing.

Thanks for your help again
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by alberto gonzalez:
I guess my question is, if the Tomcat server will cope with it as well as a JVM on its own if I was to write it as a Standalone application? This application will only have a very few people opening a JSP to monitor how the internal process is doing.


Couldn't get you, Alberto.
What do you mean by this?
alberto gonzalez
Greenhorn

Joined: Sep 05, 2005
Posts: 14
Basically that the code that I am going to be running is going to be the same regardless of whether I run it as standalone application or a webapp.

Using a web front end as the user interface gives me lots of flexibility that , due to a lack of knowledge about RMI, I cannot see using the solution suggested by you.

My question is if I can rely on TOMCAT coping as well as a Standalone app when it runs a thread(or more) with an infinite loop in the background (obviusly using sleep etc..) or if TOMCAT will freek out after a couple of days.

Thanks again
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by alberto gonzalez:
Using a web front end as the user interface gives me lots of flexibility that , due to a lack of knowledge about RMI, I cannot see using the solution suggested by you.


I think you would have more flexibilty otherwise. Web interface has lot of limitations. Its better to look at RMI or Sockets, instead.


My question is if I can rely on TOMCAT coping as well as a Standalone app when it runs a thread(or more) with an infinite loop in the background (obviusly using sleep etc..) or if TOMCAT will freek out after a couple of days.


Well, if some request take a lot of time in processing then the server suppose that the thread hung somewhere during process and then server kills that thread. However, if you respond back time by time while having your other thread scaning files, it would be ok.
[ September 21, 2005: Message edited by: Adeel Ansari ]
Caine Lai
Greenhorn

Joined: Sep 21, 2005
Posts: 15
Originally posted by Adeel Ansari:
Well, if some request take a lot of time in processing then the server suppose that the thread hung somewhere during process and then server kills that thread. However, if you respond back time by time while having your other thread scaning files, it would be ok.


Why don't you launch a background thread when Tomcat starts, with a context listener? This way you don't have to worry about the thread timing out. You could have this background thread (or threads) do their processing and store information into the application scope that the JSP threads can access to get the information they need.


Online collaboration made simple: <a href="http://www.rallypointhq.com" target="_blank" rel="nofollow">http://www.rallypointhq.com</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60979
    
  65

"Unsavory Character",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
alberto gonzalez
Greenhorn

Joined: Sep 05, 2005
Posts: 14
Thanks a lot to both of you.

I am looking at RMI but I am not going to give up with the TOMCAT solution. I have the code nearly there and now is just a question of checking how TOMCAT behaves.

Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The ServletContext