from sockets to servlets in spring web


Hi all! Again, this article is a purely basic cognitive overview for beginners. There will be nothing complicated here, so those who are hungry for complex articles, please do not throw too many tomatoes)

So, let’s say we want to write a web server with a rest-like API in java, but we don’t know any frameworks, standards, or libraries yet.

1. socket

The first thing we can do is implement all the logic ourselves completely from scratch on sockets.

Socket is a concept from TCP protocol. This is a kind of socket that can receive / send data packets over the network on a specific port.

We will need to do something like this:

• establishment/termination of TCP connections with clients

• maintaining a pool of connections so that the server can process multiple requests in parallel

• parsing http headers, cookies, urls

• and many other low-level mechanisms involving the study of protocol documentation and complex concurrency

This code can take months to implement and maintain. But even after that, such a server will be significantly inferior to the existing ones in terms of performance and stability.

In addition, the server’s business logic is likely to be drowned in technical code, making maintenance even more difficult.

An example of client-server communication via TCP protocol

An example of client-server communication via TCP protocol

2.Servlet API

To solve the problems above, the Servlet API standard was invented.

The most important part of the API method service() interface javax.servlet.Servlet:

public void service(ServletRequest req, ServletResponse res)

We just need to get the necessary parameters / headers / etc from the parameter req and write response/status to res.

In our case, it is much easier to take an abstract implementation of this interface – HttpServlet

Like that:

ru.zen.java.notes.HelloServlet class code

ru.zen.java.notes.HelloServlet class code

And bind this servlet to some path in XML:

WAR file XML configuration

WAR file XML configuration

Now, if you open http://my-api.com/hello in the browser, we will see the text “Hello from server”

Server response in browser

Server response in browser

And how does it work?

The “servlet container” does all the magic for us. This is exactly the thing that implements the servlet specification and provides us with Servlet API.

The most common:

• Tomcat

• Jetty

• Undertow

And the scheme is something like this:

1. We package our code in a special war format and load it into a container

2. For each request to the server along the specified path, the server creates a new servlet with our code

3. Each request has its own thread

Servlet container handles client requests

Servlet container handles client requests

This approach already greatly simplifies the development of a web server, but there are still a couple of important things that the framework could do for us:

• serialization/deserialization of request and response body (eg json <–> pojo)

• more flexible mapping of urls to methods (routing)

• and a bunch of more specific ‘wants’ from security to transactionality

Spring Framework

At the heart of the ecosystem, the spring is DI. This is such a thing that allows you to conveniently slip one object into another. The core turned out to be very convenient and a bunch of libraries and frameworks grew around it.

Examples:

• spring web

• spring jdbc template

• spring security

• spring cloud

• spring data

How does the spring framework work?

1. We write code in the form of simple classes (called beans)

2. We write code that describes which beans need to be injected

3. Launch IoC spring container

4. IoC container reads our code and configuration

5. According to the instructions in the configuration, slips instances of one class into another

6. We get the necessary classes from it and run methods on them

spring web

So here spring web is a web framework built on top of Servlet APIto make our lives even easier.

Code example:

ru.zen.java.notes.HelloController class code

ru.zen.java.notes.HelloController class code

This class is called controller. Its methods correspond to the URLs of our api. When the client requests http://my-api.ru/hello , the method will be executed getHello().

We described all this with the help of annotations. spring web read them and configure the servlet as needed.

It happens like this:

1. Spring parsed our controllers and set up a special class DispatcherServlet

2. Per request is triggered DispatcherServlet

3. It determines which method to call in which controller

4. In our controller method, the business logic is processed

5. The response is serialized (for example, in json)

Request handling in Spring Web

Request handling in Spring Web

Now we are happy with everything and can implement our API without thinking about anything other than business logic.

Conclusions:

1. It almost never makes sense to implement the logic of a web framework on your own – it’s long and complicated

2. Most web frameworks run on top of the Servlet API

3. It’s always good to understand how your favorite web framework works from the inside out. This helps you get the most out of it and get the most out of it.

PS: I will be glad to be active in the comments ^^

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *