JAVA SERVLETS

Mekolle Sone Gillis Ekeh Junior
2 min readSep 19, 2019

--

Simple HTTP Client Server communication architecture

So what are servlets and what do they do?

Servlets are a java technology used to handle incoming HTTP requests on a server. When an HTTP request is made to a server from browser for example, this request gets routed to Web Application Container simple called a Web Container. These web containers often contain one or more servlets. So when an http request is routed to a web container it in turn routes it to one of the servlets contained within it. The servlet then processes the HTTP request by looking at the headers and understanding how to interpret the body.

Simply, a servlet is nothing more than a Java object which contains several methods with the key methods being doGet and doPost methods each used for handling HTTP request with the GET and POST request method respectively. A java servlet is an implementation of the HttpServlet interface and implements several request handling methods that correspond to HTTP request methods.

How does the Web Container knows which servlet to route a request to?

Web containers usually use some routing function or simple a router to determine based on the incoming request, which servlet should do the handling. In Java these are configured in a file named web.xml. When a request comes from a client, the web container looks in the the web.xml file to get which servlet to use to handle the incoming request.

Conclusion

Servlets are the basic java objects used in handling incoming http request. Although creating Servlets by implementing the default HttpServlet java interface is the long way to go about building scalable Java web applications, understanding the fundamentals of how they work is very important as it is used by more advanced frameworks like Spring which has become a major tool in building scalable java web applications.

I will be posting some articles in the near future about the Spring framework and how we can use to build cloud services.

--

--

Mekolle Sone Gillis Ekeh Junior

Sone Gillis is a Telecommunications Engineer, full Stack Software Developer, certified Data Scientist and AI programmer