Introduction to Java Servlets

SHEKHAR SAXENASHEKHAR SAXENA
4 min read

Imagine you're at a restaurant, and you want to order some food. The waiter takes your order, go to the kitchen and brings the food to your table. In world of web, Servlet is like the waiter who takes your web request and brings the response from the web server.

When you visit a website, your client (for example Chrome, Firefox etc.) sends a request to the web server, just like you telling the waiter what you want to eat. The servlet, like the waiter, gets this request and figures out what you're asking for. Then, it goes to the kitchen which is the server in our case, gets the information or page you want, and brings it back to your client.

💡
Servlets are the server side Java programs, that are used to handle the request from the client, process the request on the server, produce the request, then send the response back to the client.

Decoding the Properties of Java Servlets

  • These servlets are like the superheroes of the server side. They take care of creating the web applications that give your app that dynamic, interactive content. Whether it's handling requests for HTML forms on your site or managing behind-the-scenes details like cookies and compression, servlets are on the job.

  • Servlets are not just your ordinary waiters, they are like the wizards of the Java world. They have the power to tap into the full functionality of Java class libraries, making them capable of doing some pretty amazing things. They can chat with applets, connect with databases, and even have a friendly connection with other software using sockets.

  • They're not picky about where they work. Since servlets are written in Java, they are platform-independent.

So, next time you create any web applications using Java and your clients interact with your content, just remember, behind the scenes, the servlets are the unsung heroes making it all happen!


CGI vs Servlets: Who Wears the Server Crown?

Imagine it like a magical door, when you make request to this magical door lets you talk to your friend which is on other side of the door, and your friend responds back to you again via this magical door.

💡
CGI or Common Gateway Interface is an external application that is written in any other programming language like C or C++ and that external application processes the requests and generate dynamic content.

Now let's compare CGI and Servlets.

FeatureServletsCGI
TechnologyJava-based web technologyGeneral-purpose web technology
PerformanceGenerally faster and more efficient as it does not require creation of new process for every request received.Can be slower compared to Servlets because it requires creation of new process for every new request received.
Platform IndependencePlatform-independent as it is written in Java which is a platform independent language.May or may not be as they can written in various programming language.
State ManagementSupports session management and cookiesLimited built-in support; relies on the server
Resource UtilizationEfficient resource utilization using threads.May have higher resource usage
ScalabilityTypically more scalableMay have scalability limitations
DevelopmentRequires Java programming skillsCan be written in various programming languages
SecurityProvides built-in security features using Java Security ModelSecurity is often dependent on server settings
DeploymentDeployed as part of a web applicationExecuted as a separate process by the server
Common Use CasesModern web applications, dynamic contentLegacy systems, simple web applications

The Servlet Container or Engine: Playground for Java Servlets

Imagine a servlet engine as a special playground for Java programs called servlets. It's like a big, colorful jungle where servlets can run, play, and create exciting things for websites.

The web server is like the playground supervisor. When a request comes to the website, it's the web server's job to greet them and see what clients want to do. If the request asks for something that needs a servlet's help, the web server calls on the servlet engine. The servlet engine loads up the right servlet, giving it a set of instructions to follow. The servlet gets to work, using its Java superpowers to create the content the client needs. It might fetch information from a database, generate a personalized web page, or even process a payment. Once the servlet is done, it sends the finished product back to the web server.

💡
A servlet engine is a set of objects that provides a special runtime to Java servlets. It loads the servlets, manages servlet life cycle, decode and encode MIME based requests, manages the resources, security services like authorization and authentication and sessions.
0
Subscribe to my newsletter

Read articles from SHEKHAR SAXENA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SHEKHAR SAXENA
SHEKHAR SAXENA

I am Shekhar Saxena, an Associate Software Engineer currently working at Red Hat. I completed my B.Tech in Computer Science & Engineering at Graphic Era (Deemed to be) University, Dehradun. Currently contributing as an Associate Software Engineer at Red Hat, I play a role in developing and enhancing the Kruize Autotune application.