How Servlet Actually Works

Java Servlet

Servlet is a Java class which runs on a web server where application resides. This is a middle layer between web browser from where HTTP request originate and application web server.

Servlet helps us to get data submit by end users using POST request and return data to web browser using GET request from application web server.

Java web application J2EE helps us to create servlet classes and deploy on web server in the form of war file.

Java Servlet classes related classes belongs to javax.servlet and javax.servlet.http packages to implement servlet program.

HttpServlet

HttpServlet class is an abstract class belongs to javax.servlet.http.HttpServlet package and extends GenericServlet abstract class. We can create servlet using HttpServlet class. HttpServlet methods GET, POST, PUT etc. allows us to handle HTTP requests.

Over all flow / Architecture

Below architecture is all about send data from web browser to servlet program and back from servlet program to web browser.

  • The request will get originate from web browser to get and send data from to server.
  • Request will use HTTP or HTTPS protocol to get or send data to server.
  • The request from web browser will first reach to web server(apache tomcat, JBoss, etc.).
  • Web server will call specific servlet program depending on servlet URL mapping, request type(GET, POST, DELETE etc. ), content type etc.
  • Web browser can send or get data from server in predefined formats such as JSON, XML, binary etc.
  • Servlet class will collect data from web browser. Servlet class will process the data collected from browser. Servlet class will and save/store same data in database. Servlet class can also collect data from database and send back to browser in same predefined format.
  • Servlet class will process data using third party API data and other classes within application server.

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0