Can we create a server in Java application without creating Spring or any other framework?

Yes, it is completely possible to create a server in Java without using Spring, Jakarta EE, or any other framework. Java provides built-in APIs that allow developers to create TCP, UDP, and HTTP servers directly using core Java classes. Frameworks mainly simplify development, but they are not mandatory for building server applications.

Key Points: • Java SE provides built-in classes for creating servers. • ServerSocket can be used for TCP-based communication. • HttpServer can be used to create lightweight HTTP services. • Frameworks like Spring add features but are built on top of core Java concepts. • Understanding core server development helps in learning how frameworks work internally.

How Can We Create a Server in Core Java?

Java provides several options:

1. ServerSocket

Used for creating TCP servers.

Common Use Cases:

• Chat applications • Socket-based communication • Multiplayer games • Custom protocols

Example Flow:

Client | Socket Connection | ServerSocket | Server Processes Request

2. HttpServer

Java provides a lightweight HTTP server implementation through:

com.sun.net.httpserver.HttpServer

Common Use Cases:

• Simple REST APIs • Internal tools • Prototypes • Learning HTTP concepts

3. DatagramSocket

Used for UDP-based communication.

Common Use Cases:

• Streaming applications • Real-time gaming • Broadcasting systems

Example: Suppose we want to build a simple web server that returns:

"Hello World"

This can be done using Java's built-in HttpServer without Spring Boot.

Code Example:

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class SimpleServer {

    public static void main(String[] args)
            throws IOException {

        HttpServer server =
                HttpServer.create(
                        new InetSocketAddress(8080),
                        0);

server.createContext("/hello",

                (HttpExchange exchange) -> {

                    String response =
                            "Hello World";

                    exchange.sendResponseHeaders(
                            200,
                            response.length());

                    OutputStream os =
                            exchange.getResponseBody();

                    os.write(response.getBytes());

                    os.close();
                });

        server.start();

        System.out.println(
                "Server started on port 8080");
    }
}

Request:

http://localhost:8080/hello

Response:

Hello World

Creating a TCP Server Using ServerSocket

Code Example:

import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {

    public static void main(String[] args)
            throws Exception {

        ServerSocket serverSocket =
                new ServerSocket(8080);

        System.out.println(
                "Waiting for client...");

        Socket client =
                serverSocket.accept();

        System.out.println(
                "Client Connected");
    }
}

This creates a basic TCP server that waits for client connections.

Core Java Server vs Spring Boot

Feature Core Java Spring Boot

HTTP Server Manual Built-In

Dependency Injection No Yes

REST API Support Manual Automatic

Configuration Manual Simplified

Learning Curve Lower Higher

Development Speed Slower Faster

Enterprise Features Limited Rich

When Should You Use Core Java?

• Learning networking fundamentals • Building lightweight tools • Creating custom protocols • Understanding server internals • Interview demonstrations

When Should You Use Spring Boot?

• Enterprise applications • Microservices • REST APIs • Production systems • Large-scale applications

Real-World Example

Spring Boot itself does not create networking concepts from scratch.

Internally it relies on:

• Java Networking APIs • Embedded servers such as Tomcat, Jetty, or Undertow • JVM threading and socket mechanisms

This means frameworks simplify development, but the underlying server concepts still come from Java.

Interview Tip: A concise interview answer is:

"Yes, a server can be created in pure Java without any framework. Java provides built-in APIs such as ServerSocket for TCP communication and HttpServer for HTTP services. Frameworks like Spring Boot simplify server development by providing additional features such as dependency injection, REST support, and configuration management, but they are not required to create a server."