Solved

servlet context

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

18 Experts available now in Live!

Get 1:1 Help Now