Servlet and JSP


Servlets are Java classes that extend HttpServlets and it is used in server-side to create a dynamic web page. These special classes are able to handle requests from clients.HttpServlet extends servlet interface which has five abstract methods among them init(): is used for initialization of servlet, service(): is used to give an appropriate response, destroy(): is used to destroy servlet and close opened resources.

  1. Servlet class initialization: After loading, the container initialized the servlet context object for the servlet and then invoke its init() method and pass the context object to initialize variables.
  2. Request Handling: Once the servlet initialized it ready to handle client request and invoke service method with request and response object is passed by the web container. Create a separate thread for every request.
  3. Remove servlet: destroy() method is invoked.
  1. Session scope: Session scope is specific to the browser, in the same session we can access parameters within any servlet and use those values.
  2. Application scope: We can set key-value pairs in context scope and use those within the whole application. We can configure context in deployment descriptor ie: web.xml or by using context instance context.setAttribute(key,value).
  1. Developers enjoy JSP implicit objects that are automatically available to developers by web container, and they can be called directly without being explicitly declared.
  2. In JSP we can use custom tags which reduce the length of code.
  1. Declaration tag: We can write normal java code inside this tag but those codes will be placed outside the service method after JSP to Servlet conversion syntax:<%! java code %>
  2. Expression tag: It evaluates and converts the expression into a string. Syntax: <%= java code %>
  3. Directive tag: It is used to import a package or to include other JSP code or we can declare external tags. syntax: <%@ %>



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store