aspose file tools*
The moose likes Servlets and the fly likes Persistent data in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Persistent data in servlets" Watch "Persistent data in servlets" New topic
Author

Persistent data in servlets

Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
Hi,

So is there any way we can maintain persistent data in servlets?

Since tomcat acts as a container for running my servlet data, each time a connection is invoked, tomcat runs a new instance of the servlet on a different thread.

Say I want to hold some value in a variable and dont want to reinit but use it again on the next instance, is there some way we can do that ?

Regards,
Bharath
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Bharath Neo wrote:So is there any way we can maintain persistent data in servlets?
No. See below.

Since tomcat acts as a container for running my servlet data, each time a connection is invoked, tomcat runs a new instance of the servlet on a different thread.

No, it doesn't. It uses the same servlet instance in mutltiple threads servicing multiple requests. Which is why you cannot use servlets themselves as a peristance mechanism.

Say I want to hold some value in a variable and dont want to reinit but use it again on the next instance, is there some way we can do that ?

Of course. That's what the various scopes are all about. If you want data stored on a per-user basis, use the session. if it's application-wide, use the application context.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
Thanks for the info. But looks like the application context has to be hard coded in the web.xml file. Cant we change it or set it from the servlet code?

Basically I dont want to connect to the DB always from the tomcat on every connection but just once.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Bharath Neo wrote:Thanks for the info. But looks like the application context has to be hard coded in the web.xml file.

Huh? The application context exists for every web application. You don't have to do anything in the deployment descriptor.

Cant we change it or set it from the servlet code?

If you mean: can you store and change information in the application context, then yes.

Basically I dont want to connect to the DB always from the tomcat on every connection but just once.

For what purpose? You don't want to create a single connection and then use it throughtout the life of the web application. That's exactly the wrong way to do it. Rather, a connection pool that grabs connections only when needed, and releases them as soon as possible is the best way to go.
Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
Thanks for the info. Shall look at connection pools. But is there any code sample for using the application context? Googling returns it mostly as the context info the deployment desc file...
Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
Also cant I just have define it in the init() method of the servlet to make it global for the whole lifetime tomcat stays up?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Bharath Neo wrote:But is there any code sample for using the application context?

It's just a tad confusing because programmatically, it's the servletContext. To set a scoped variable into application context: to fetch one:

Bharath Neo wrote:Also cant I just have define it in the init() method of the servlet to make it global for the whole lifetime tomcat stays up?

You can, but it's a bad bad idea. DB resource are precious and should remain open only as long as they are absolutely needed.

It's sort of like borrowing a library book. While you have the book out, nobody else can borrow it. So it's most polite to only reserve the book while you are reading it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Persistent data in servlets