Understanding a servlet's life cycle
If you are used to creating web applications using Java EE, you probably will have already realized: most of the time it is all about dealing with requests and responses and the most popular way to do it is by using the Servlet API.
This recipe will show you how the server deals with its life cycles and what you should and should not been doing in your code.
Getting ready
First, add the proper dependency to your project:
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>8.0</version> <scope>provided</scope> </dependency>
How to do it...
Just write this simple servlet:
@WebServlet(name = "LifecycleServlet",
urlPatterns = {"/LifecycleServlet"})
public class LifecycleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException...