How it works...

If you pay attention, you will notice that the init log will show only after your servlet is called for the first time. That's when it is really loaded and it is the only time that this method will be called. So if you have some one-shot code for this servlet, that's the place to do it.

Talking about the doGet, doPost, doPut, and doDelete methods, note that they were all automatically called by the server based on the request received. It's possible thanks to another method implemented by the server called service.

You could override the service method if you want, but it's a bad practice and should be avoided. Do it only if you know exactly what you are doing, otherwise you could give the wrong destination to some requests. ...

Get Java EE 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.