Solved

servlet context

Posted on 2003-12-02
5
756 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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