In below there is question and answer, What I need is I want comment about the a
ID: 3592551 • Letter: I
Question
In below there is question and answer, What I need is I want comment about the answer( feel free to agree or disagree with answer), and 2nd thing is give your own answer for the question(something diffrent than answer)
Question:
Why is it a good idea to do the database initialization in the init() method rather than the doGet() or doPost()? Discuss.
Answer:
The init() method is called to read the server-specific initialization parameters, among other things. These parameters may include the different types of database settings, passwords, performance parameters, etc. The init() method is called when the Servlet is created for the first time and isn't called again for any other requests. In contrast, the doGet() and doPost() methods are called often for several types of user requests. Since the database only has to get initialized once, to me it makes sense for the database initialization to occur in the init() method instead of any other methods that are called frequently.
Explanation / Answer
Yes, I agree. Database initialization should be in init() while opening and closing the connection to database could be performed anywhere. Multiple database initializations will start multiple instances of the database which will introduce ambiguities. For all the servlet operations only one database is enough to serve which should be initialized in the beginning of the servlet life cycle which is inside init() whereas connection to this database instance can be made from anywhere i.e. one can open and close the connection in init() or in each doXXX() methods or can use combination of init()/ destroy() to open & close the database connection or open/close database connection once per session.
If this helps please give a thumbs up. Good Luck!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.