Solved

servlet context

Posted on 2003-12-02
5
728 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
handling Struts tag, JSTL tag and scriptlet in Ajax. 7 170
jersey maven project error 7 137
Clear browser cache on site login, is it possible? 3 24
maven set up 2 126
Fine Tune your automatic Updates for Ubuntu / Debian
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now