Introduction to Servlets

Servlets acts as the server-side heroes that processes the requests from the users’ web browser and sends back the response after the required manipulation is completed. Imagine it to be a communication channel between your server and users’ web browsers.

  • Working of Servlets: Servlets catches the requests when the user interacts with the web application either let say, by clicking the button or submitting the form, process them using Java code, and then send a response back to the user’s browser.
  • Servlet Lifecycle: Servlets lifecycle can be compared to a human one where they wake up during initialization, their active life will go on with handling requests, and say their goodbyes when the server is shutting down.

Popular Containers (In-Built Servlets)

Consider your web browser to be a house and the requests to be someone taking care of the tasks. So you can imagine the someone taking care of the tasks as the superhero which is nothing but a servlet container in the Java web world, such as Tomcat or Jetty.

They come pre-equipped with a bunch of built-in servlets which are somewhat ready-to-use, efficient, and optimized like smart butlers. You don’t have to create them; they’re already there to handle common tasks like serving static files, managing sessions, or handling error pages. It’s like assuming a team already present inside your house to complete the tasks as and when they receive.

Manual Servlets

For some additional tasks where maybe, you want a personalized caretaker who does things exactly the way you like, this is where in-built servlets lack and exactly that’s where manual servlets come in.

It is somewhat like hiring your own superhero. You write Java code for the servlet behavior, you decide the tasks, how it processes requests, and what responses it sends back, getting a full control over the things.

Example:

Imagine you want a servlet that welcomes users with a “Hello World!” message. You’d write a bit of Java code, compile it, set it up in your web application, and voila! Now, every time a user interacts with your servlet, it responds with a friendly greeting.

Java




import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
  
// GFG extends HttpServlet, indicating it's a servlet
public class GFG extends HttpServlet {
  
    // Handles HTTP GET requests
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          
        // Response content type has set to HTML
        response.setContentType("text/html");
  
        // Get the PrintWriter object to write HTML response
        PrintWriter out = response.getWriter();
  
        // Write the HTML response with a greeting
        out.println("<html><body>");
        out.println("<h2>Hello World!</h2>");
        out.println("</body></html>");
    }
}


Servlets are like those workers who works backstage of the web applications. Right from handling requests, they process information, and make sure everything runs smoothly behind the application making it more user friendly. Whether it’s saying, “Hello World!” or managing complex operations, servlets are there to make your web applications come to life.

Advanced Java – Getting Started to Web Application

A Web Application is software that executes on the web browser. It links the customers and the service providers as a medium of exchange. For example, Amazon provides a marketplace where sellers and buyers can coexist. It provides a mechanism to exchange services and perform operations seamlessly.

Similar Reads

Understanding Web Clients and Servers

For any operation to be performed, a service provider and a service consumer need. The same relies on web applications. The one which provides the service is the web server and the one which requires it is the web client. The web client requests the client, in return for which the client responds. This is usually done through HTTP protocol and that’s the reason, why sometimes a web server is also known as an HTTP server....

Client-Server Communication

Client-server communication is usually done through TCP/IP protocol. TCP covers part of transport and the sessions layer in a typical OSI model. TCP being a connection-oriented protocol makes sure that the connection remains in place till the messages are exchanges at the very end of the queue....

Introduction to Servlets

...

Web Application Architecture

Servlets acts as the server-side heroes that processes the requests from the users’ web browser and sends back the response after the required manipulation is completed. Imagine it to be a communication channel between your server and users’ web browsers....

Java Web Frameworks

...

Handling HTTP requests and responses in Java servlets

1. Monolithic Architecture...

When to Choose a Framework

Java web frameworks acts like toolkits for building web applications. These frameworks provide you with a set of rules to make your job easier, you are being the master for the application. Two popular ones are Spring MVC and its close friend, Hibernate....