i am using a Servlet Context Listener for an assignment in college several of the context variables are set from the database. but when i add records to those lists in the database they do not update. is their a way this can be achieved?
ServletContextListner:
import edu.witc.pethotel.business.DispositionType;
import edu.witc.pethotel.business.Gender;
import edu.witc.pethotel.business.PetType;
import edu.witc.pethotel.business.State;
import edu.witc.pethotel.data.CustomerDB;
import edu.witc.pethotel.data.TypeDb;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
//initialize customer service email
String custServEmail = sc.getInitParameter("custServEmail");
sc.setAttribute("custServeEmail", custServEmail);
//initialize current year
GregorianCalendar currentDate = new GregorianCalendar();
int currentYear = currentDate.get(Calendar.YEAR);
sc.setAttribute("currentYear", currentYear);
//initialize list of gender
ArrayList<Gender> genders = TypeDb.getAllGenders();
sc.setAttribute("genders", genders);
//initialize list of disposition
ArrayList<DispositionType> dispositions = TypeDb.getAllDispositions();
sc.setAttribute("dispositions", dispositions);
//initialize list of state
ArrayList<State> states = CustomerDB.getActiveStates();
sc.setAttribute("states", states);
//initialize list of pet type
ArrayList<PetType> petType = TypeDb.getActivePetTypes();
sc.setAttribute("petTypes", petType);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
sc.removeAttribute("genders");
sc.removeAttribute("dispositions");
sc.removeAttribute("states");
sc.removeAttribute("petTypes");
}
}
this method can be used as a workaround inside a servlet after changing the database just overwrite the context variables that are changed
request.getServletContext().setAttribute("states", TypeDb.getActiveStates());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.