Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to build Servlet Containers in Java

In this article we will see how to create servlet containers and explain the functioning of a simple servlet container.

We have a servlet container applications presented over here and is tried to be kept as simple as possible. The reason for not trying to make the first container perfect is because we want to keep it simple.

Simple servlets can be processes by both the servlet containers and static resources as well. In order to test this container, make use of PrimitiveServlet which is located in the webroot/ directory and the complex servlets are not within the reach of the capability of these containers.

In order to grasp the knowledge on how around how the applications work, one needs to be familiar with thejavax.servlet.Servlet interface. Let us discusses this interface first and then the role of a servlet container serves as a servlet.

Introduction to javax.servlet.Servlet Interface

There are classes and interface of two packages: javax.servlet and javax.servlet.http with the help of which Servlet programming is made possible. Amongst these classes and interfaces, the javax.servlet.Servlet interface is one of the most important interfaces that all Servlets must implement.

The below lists the signature of the Servlet interface that has five methods:

  • public void init(ServletConfig config) throws ServletException
  • public void service(ServletRequest request, ServletResponse response)
  • throws ServletException, java.io.IOException
  • public void destroy()
  • public ServletConfig getServletConfig()
  • public java.lang.String getServletInfo()

The init, service, and destroy methods are considered as the servlet's lifecycle methods.

  • The init method is called once by the servlet container. This is done post the servlet class has been instantiated to point to the servlet about its placement into service.
  • It is a must for the init method to successfully complete successfully before any request can be received by the servlet.
  • The service method is then called by the servlet container. This is just o permit the servlet to respond to a request. We have a servlet container that passes a javax.servlet.ServletRequest object and ajavax.servlet.ServletResponse object.
  • The ServletRequest object comprises of the client's HTTP request details and then we have ServletResponse encapsulating the servlet's response. The purpose of these two objects is to allow you to write custom code that would help in identifying how the servlet services the client request.
  • There comes a servlet container calls that destroy method before removing a servlet instance from service that usually takes place when the servlet container is either shut down or when the servlet container requires some free memory.

Below coding reflects the code for a servlet named PrimitiveServlet. This is a very simple servlet that can be used to test the servlet container applications. The PrimitiveServlet class is used to implement javax.servlet.Servlet that all the servlets must to do and then provides implementations for all five servlet methods. Each time any of the init, service, or destroymethods is called, we have a servlet that writes the method's name to the console.

Listing 1: Representing the code for PrimitiveServlet

PrimitiveServlet.java
import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

public class PrimitiveServlet implements Servlet {
    public void init(ServletConfig config) throws ServletException {
        System.out.println("init");
    }

    public void service(ServletRequest request, ServletResponse response)
        throws ServletException, IOException {
        System.out.println("from service");
        PrintWriter out = response.getWriter();
        out.println("Hello. Roses are red.");
        out.print("Violets are blue.");
    }

    public void destroy() {
        System.out.println("destroy");
    }

    public String getServletInfo() {
        return null;
    }

    public ServletConfig getServletConfig() {
        return null;
    }
}

Our Application:

  • Make sure that when the servlet is called for the first time, you need to load the servlet class and call its init method and that only once.
  • For each and every request, there is a need to construct an instance of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse.
  • The servlets’s service method can be invoked by passing the ServletRequest and ServletResponse objects.
  • In case of shut down of servlet class, you need to call the servlet's destroy method and unload the servlet class.

The simple servlet container is not fully functional thereby it runs only simple servlets. Also it does not call the servlet's init and destroy methods and does the following:

  • Wait for HTTP request; Construct a ServletRequest object and a ServletResponse object.
  • And now if the request is for a static resource, you can invoke the process method of the StaticResourceProcessor instance. This can be done by passing theServletRequest and ServletResponse objects.
  • In case we have a request that is for a servlet, you need to load the servlet class and invoke its service method. This can be done by passing the ServletRequest and ServletResponseobjects.

The point to note here is that the servlet class in a servlet container is loaded every time the servlet is requested.

We have the servlet container that comprises of six classes in our application discussed above:

  • HttpServer1
  • Request
  • Response
  • StaticResourceProcessor
  • ServletProcessor1
  • Constants

The entry point of this application (application here refers to the static main method) is in the HttpServer class and the purpose of this method is to create an instance of HttpServer and calls it’s await method. The method waits for HTTP requests, creates a Request object and a Response object, and the finally transfers either to a StaticResourceProcessor instance or a ServletProcessor instance. This depends on whether the request is for a static resource or a servlet.



Software developer with more than 5 years of development on Java, HTML, CSS.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login