Java Interview Question: What Is HTTP?
Tags: interview
Published: 2023-04-11 10:14:13

HTTP is one of the many concepts a Java Engineer should know.

Since Java is mostly suited for server-side operations, understanding the concept behind HTTP will be valuable during your role as a Java Engineer.

## What is HTTP?

HTTP stands for Hyper Text Transfer Protocol.

It's a stateless request-response protocol.

> HTTP is an [application layer](https://www.cloudflare.com/learning/ddos/application-layer-ddos-attack/) protocol designed to transfer information between networked devices and runs on top of other layers of the network [protocol](https://www.cloudflare.com/learning/network-layer/what-is-a-protocol/) stack.[[Cloudflare](https://www.cloudflare.com/learning/ddos/glossary/hypertext-transfer-protocol-http/)]

### What does stateless mean?

**Stateless** means that each request is independent of another.

HTTP being stateless plays a significant role in how software engineers design web applications.

> An example of a stateless transaction would be doing a search online to answer a question youve thought of. You type your question into a search engine and hit enter. If your transaction is interrupted or closed accidentally, you just start a new one. Think of stateless transactions as a vending machine: a single request and a response. [[Red Hat]](https://www.redhat.com/en/topics/cloud-native-apps/stateful-vs-stateless)

### What are the advantages of a stateless HTTP?

HTTP being stateless:

- Simplifies server design.

- Reduces the demand on server resources because we don't need storage to track clients (although most servers will log traffic).

As soon as a request is fulfilled, the connection drops.

The server doesn't preserve unused connections.

HTTP stateless protocol allows each server thread to manage other requests continually.

The ability to handle multiple requests allows the scale of many clients, and each request is guaranteed to be serviced.

This independence simplifies the assembly of pages from multiple resources (such as images, media, etc.).

Resources can come from different origins, and load can be distributed across multiple servers.

HTTP requests being cached:

- Improve the speed of response to the user.

- Reduces network and server loads, allowing the Web to scale.

HTTP being stateless will enable applications to be resilient to network failure.

Since the connections drop once the request has been fulfilled, a network glitch won't cause a loss of state. This advantage is crucial for mobile applications.

> **Related:** [Java Interview Questions](https://techwithmaddy.com/java-interview-questions)

## GET vs POST

GET and POST are highly used types of HTTP requests.

GET request:

- Appends name-value pairs to the URL.

- It's considered safe.

- It's the default behaviour of clients and proxies to cache responses.

- URL can be easily bookmarked.

POST request:

- Sends name-value pairs after the HTTP header.

- It's considered to be unsafe because the response cannot be cached.

- It can carry a larger [payload](https://www.techtarget.com/searchsecurity/definition/payload#:~:text=In%20computing%2C%20a%20payload%20is,malicious%20code%20to%20do%20damage.).

## State Preservation

State preservation means how data gets stored when performing an HTTP request.

It happens in three basic variations:

### Cookies

- Store a small amount of information on the client.

- Sent to the server at each HTTP request.

### Session variables

- A unique identifier associates information stored on the server with a particular client.

### Passing data at each request-response cycle

- Store information on the web page.

- Appending data to a URL.

- Hidden fields in HTML forms.

> **Related** : [Why We Need Architectural Design In Software Engineering](https://techwithmaddy.com/why-we-need-architectural-design-in-software-engineering)

## Key Takeaways

In this article, you've learned:

- What HTTP is.

- What stateless means.

- The advantages of a stateless HTTP.

- GET vs POST requests.

- Basic variations of state preservation.

If you found this article helpful, you can subscribe to my [FREE weekly newsletter](https://techwithmaddy.com/newsletter).

Until next time!

🙋🏾