In this blog we are going to learn about servlets and jsp and what is the requirement of them.

Photo by Desola Lanre-Ologun on Unsplash

Need of Servlet and JSP


You can create a servlet by extending GenericServlet or HttpServlet. This interface characterizes techniques to introduce a servlet, to support demands, and to eliminate a servlet from the worker.

Life Cycle Of Servlet

  1. A servlet is created, then initialized.
  2. Calls from the client are handled and sent back a response.
  3. Servlet is taken out of service, then destroyed and garbage collected.

Methods Of Servlet

1. init()

The init method must be completed successfully before the servlet starts receiving any requests.



Example Of Servlet


JSP is different from the servlet as here we embed the Java code using scripting tags like

  1. <% — comment — %> To add comments
  2. <%@ directive %> To import classes and include pages
  3. <%! declarations %> declare variables in class outside service method
  4. <% scriplet %> To add Java code
  5. <%= expression %>To print Java expressions

LifeCycle Of JSP

  1. Loading the servlet class and creating a servlet instance.
  2. Initializing the servlet, processing the request and destroying the servlet.

Example Of JSP