A servlet class is injected with a JDBC data source. After injection has occurred, the servlet needs
to create a cache out of some of the data in the database, so as to improve responsiveness.
Which two methods can host the cache creation code? (Choose two)
A.
Servlet.init()
B.
Servlet.destroy()
C.
A method annotated with @Init
D.
A method annotated with @PostConstruct
E.
A method annotated with @PreDestroy
F.
A method annotated with @Resource
Explanation:
A: Because the Servlet init() method is invoked when the servlet instance is loaded,
it is the perfect location to carry out expensive operations that need only be performed during
initialization. By definition, the init() method is thread-safe. The results of operations in the
HttpServlet.init() method can be cached safely in servlet instance variables, which become readonly in the servlet service method.
D: Example:
@PostConstruct
private void init() {
cached = (Cached) ctx.lookup(EJB_PATH + Cached.class.getSimpleName());
A,D