This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Servlets are intended to run in a "servlet container" just like applets run in an "applet container". A servlet container manages creation of a servlet instance and calls init with a ServletConfig so that you can initialize your code AFTER the container has handled its responsibilities. Bill
I always use the init() method when i have to connect to the database for example...because it wastes time to connect it..Then init() is usefull to do some things before.
init method is called once and before any of the request to the servlet are serviced. all that is required by the servlet that are common across requests are initialized here..like creation og DB connection....
It is impossible to make anything foolproof because fools are so ingenious..Murphy<br />Basu.
Vasantha Prabha
Ranch Hand
Joined: Oct 02, 2003
Posts: 108
posted
0
ok I accept we need init(ServletConfig config) method to initialize initparameter.why do we need this init() method.why can't we can use init(servletConfig config) itself for all initialization things Regards, Sangeetha
This is to allow your code to be simpler if you need less features of the servlet API. In some cases, and application will need to access the servlet config during init. In this case it makes sense to use the more complex init(ServletConfig config). Remember, though, that you must make sure to call "super.init(config) before the rest of your init processing - this is often missed, and can lead to all sorts of confusion later. If your application does not need to access the ServletConfig during init, it makes much more sense to just implement the simpler init() method. By the time the comtainer calls this, it has already done the equivalent of super.init(config) for you, so you don't need to worry about forgetting it.
Originally posted by Engin Okucu: I always use the init() method when i have to connect to the database for example...because it wastes time to connect it..Then init() is usefull to do some things before.
I hope you don't mean you're creating Connections in the init() and keeping those alife as globals? That's extremely poor practice and will work only with extremely low load.
if you want you can have your servlet, "xyz" extend a "baseXyz" servlet. The baseXyz can contain the init() and hide alot of that "one-time" set up code. Once you get those init parts working well you can kind of forget about the base class and concentrate on doGet(), etc.
"No one appreciates the very special genius of your conversation as the dog does."
Vasantha Prabha
Ranch Hand
Joined: Oct 02, 2003
Posts: 108
posted
0
If your application does not need to access the ServletConfig during init, it makes much more sense to just implement the simpler init() method. By the time the comtainer calls this, it has already done the equivalent of super.init(config) for you, so you don't need to worry about forgetting it. ************************************************************************** Heyyy If the init() method internally call the super.init(config) then why we need this init(SerlvetConfig config) method???.We can directly get the getInitParameter() in all the method? Regards, Sangeetha
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.