Solved

servlet context

Posted on 2003-12-02
5
746 Views
Last Modified: 2010-04-01
Hi Experts,

I am currently storing a few lists such as contry codes and languages etc in the servlet context when the server starts up.
This helps me in saving unnecessary connections to the database.

Can I update the servlet context if I add a field in the database without stopping the server.
by accessing it like a session variable.

Kindly advise.
Thank you for your help
0
Comment
Question by:applekanna
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 75 total points
ID: 9858949
Yes, assuming you have one instance of the servlet which loads the information (singleton), you can store this instance in a static variable inside the servlet, then call this method when the database is updated to update your records...

OR, you can have a TimerTask which runs every hour or so, connects to the database, and refreshes the list

or a combination of the two...

can you post your code which does the servlet context work at the moment...we should be able to extend it

Also, is the database being updated via the same webapp?  Or are you putting new values in from another application (or the SQL console) ?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9858967
what do you mean update? add variables? change values? if so... then yeah.

just do as you would w/ the session object:

from either a jsp or servlet:

getServletContext().setAttribute(<key>, <val>);

0
 
LVL 3

Author Comment

by:applekanna
ID: 9859070
Oops sorry for not making the quetsion clear.

I have a list of country names in my appllication servlet context loaded from the DB.
If my manager adds a new country to the table I should be able to show that too without stopping the server.

What I had in mind was
 at a singleton init servlet  load the servlet context

whenver a record is added or removed, remove the servelt context variable , reload it from db and then set  it again.

like session.getAttribute("countryList");


      session.removeAttribute("countryList");
//load country list vector from db and then
      session.setAttribute("countryList");
Similary can I do with servelt context . Is there any issues I need to be aware of ..

Thank u.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9859212
> What I had in mind was at a singleton init servlet  load the servlet context

Yup :-)

> whenver a record is added or removed, remove the servelt context variable , reload it from db and then set  it again.

Reload from db into list
replace attribute

servlet.getServletContext().setAttribute( "countryList", list ) ;

if you replace instead of reove, get then add, you won't get that short period where there is no list :-)

0
 
LVL 3

Author Comment

by:applekanna
ID: 9860160
thx for your comments :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question