Skip to content

Commit 8239a85

Browse files
committed
2 parents b82f052 + 642e019 commit 8239a85

File tree

51 files changed

+5006
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5006
-79
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ BASE_URL =
22
ALGOLIA_API_KEY =
33
ALGOLIA_APP_ID =
44
GTM_ID =
5-
GA_ID =
5+
GA_ID =

docs/SQL/Introduction-to-the-SQL.md

Lines changed: 0 additions & 57 deletions
This file was deleted.

docs/WebSockets.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: websockets-for-real-time-communication
3+
title: WebSockets for Real-Time Communication
4+
sidebar_label: Web Sockets
5+
---
6+
7+
## Introduction
8+
9+
WebSockets are a powerful technology for enabling real-time communication between clients and servers...
10+
11+
## WebSocket API
12+
13+
The WebSocket API provides methods and events for establishing and managing WebSocket connections...
14+
15+
## WebSocket Protocol
16+
17+
Unlike HTTP, WebSockets provide full-duplex communication over a single, long-lived connection...
18+
19+
## Implementing WebSockets
20+
21+
### Using WebSocket in Node.js
22+
23+
```javascript
24+
// Example Node.js WebSocket server
25+
// Example Node.js WebSocket server
26+
27+
const WebSocket = require('ws');
28+
29+
// Create a WebSocket server instance
30+
const wss = new WebSocket.Server({ port: 8080 });
31+
32+
// WebSocket server listening event
33+
wss.on('listening', () => {
34+
console.log('WebSocket server is listening on port 8080');
35+
});
36+
37+
// WebSocket connection event
38+
wss.on('connection', (ws) => {
39+
console.log('A new client connected');
40+
41+
// Send a welcome message to the client
42+
ws.send('Welcome to the WebSocket server!');
43+
44+
// WebSocket message event
45+
ws.on('message', (message) => {
46+
console.log(`Received message => ${message}`);
47+
48+
// Broadcast the received message to all clients
49+
wss.clients.forEach((client) => {
50+
if (client.readyState === WebSocket.OPEN) {
51+
client.send(`Broadcast: ${message}`);
52+
}
53+
});
54+
});
55+
56+
// WebSocket close event
57+
ws.on('close', () => {
58+
console.log('Client disconnected');
59+
});
60+
61+
// WebSocket error event
62+
ws.on('error', (error) => {
63+
console.error(`WebSocket error: ${error}`);
64+
});
65+
});
66+
67+
// Handle WebSocket server errors
68+
wss.on('error', (error) => {
69+
console.error(`WebSocket server error: ${error}`);
70+
});
71+
```
72+
73+
Client-Side Example:
74+
To interact with this WebSocket server from a client, you would typically use JavaScript in a web browser:
75+
76+
```html
77+
<!DOCTYPE html>
78+
<html lang="en">
79+
<head>
80+
<meta charset="UTF-8">
81+
<title>WebSocket Client</title>
82+
<script>
83+
const ws = new WebSocket('ws://localhost:8080');
84+
85+
// WebSocket open event
86+
ws.addEventListener('open', () => {
87+
console.log('Connected to WebSocket server');
88+
});
89+
90+
// WebSocket message event
91+
ws.addEventListener('message', (event) => {
92+
console.log(`Received message from server: ${event.data}`);
93+
});
94+
95+
// WebSocket close event
96+
ws.addEventListener('close', () => {
97+
console.log('Disconnected from WebSocket server');
98+
});
99+
100+
// Function to send a message to the WebSocket server
101+
function sendMessage() {
102+
const message = document.getElementById('message').value;
103+
ws.send(message);
104+
}
105+
</script>
106+
</head>
107+
<body>
108+
<h1>WebSocket Client</h1>
109+
<input type="text" id="message" placeholder="Enter message">
110+
<button onclick="sendMessage()">Send</button>
111+
</body>
112+
</html>
113+
```
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
id: overview-of-requestparam
3+
title: Overview of @RequestParam in Spring Boot
4+
sidebar_label: Overview
5+
---
6+
7+
In Spring Boot, the `@RequestParam` annotation is used to extract query parameters, form parameters, and parts of multi-part requests from the URL. This annotation is part of the Spring Web module and binds HTTP request parameters to controller method arguments.
8+
9+
## Usage
10+
11+
The `@RequestParam` annotation can be used in a controller method to extract parameters from the query string of the URL.
12+
13+
### Example
14+
15+
```java
16+
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
@RestController
21+
public class MyController {
22+
23+
@GetMapping("/greet")
24+
public String greet(@RequestParam(name = "name", defaultValue = "World") String name) {
25+
return "Hello, " + name + "!";
26+
}
27+
}
28+
```
29+
30+
In this example:
31+
- A GET request to `/greet?name=John` returns `"Hello, John!"`.
32+
- A GET request to `/greet` returns `"Hello, World!"` due to the default value.
33+
34+
## Required Parameters
35+
36+
By default, `@RequestParam` is required. If the parameter is not present in the request, Spring will throw an exception.
37+
38+
### Example
39+
40+
```java
41+
@GetMapping("/greet")
42+
public String greet(@RequestParam String name) {
43+
return "Hello, " + name + "!";
44+
}
45+
```
46+
47+
If the `name` parameter is missing in the above example, Spring will throw a `MissingServletRequestParameterException`.
48+
49+
## Optional Parameters
50+
51+
To make a request parameter optional, set the `required` attribute to `false`.
52+
53+
### Example
54+
55+
```java
56+
@GetMapping("/greet")
57+
public String greet(@RequestParam(required = false) String name) {
58+
return "Hello, " + (name != null ? name : "World") + "!";
59+
}
60+
```
61+
62+
## Default Values
63+
64+
You can provide a default value for a parameter using the `defaultValue` attribute. If the parameter is not present in the request, the default value will be used.
65+
66+
### Example
67+
68+
```java
69+
@GetMapping("/greet")
70+
public String greet(@RequestParam(name = "name", defaultValue = "World") String name) {
71+
return "Hello, " + name + "!";
72+
}
73+
```
74+
75+
## Multiple Parameters
76+
77+
You can use multiple `@RequestParam` annotations in a single method to bind multiple parameters.
78+
79+
### Example
80+
81+
```java
82+
@GetMapping("/add")
83+
public String add(@RequestParam int a, @RequestParam int b) {
84+
return "Sum: " + (a + b);
85+
}
86+
```
87+
88+
A GET request to `/add?a=5&b=3` would return `"Sum: 8"`.
89+
90+
## List Parameters
91+
92+
If a request parameter can have multiple values, you can bind it to a `List`.
93+
94+
### Example
95+
96+
```java
97+
@GetMapping("/numbers")
98+
public String numbers(@RequestParam List<Integer> numbers) {
99+
return "Numbers: " + numbers;
100+
}
101+
```
102+
103+
A GET request to `/numbers?numbers=1&numbers=2&numbers=3` would return `"Numbers: [1, 2, 3]"`.
104+
105+
## Conclusion
106+
107+
The `@RequestParam` annotation in Spring Boot is a powerful tool for extracting parameters from HTTP requests. It allows for required and optional parameters, default values, and can handle multiple and list parameters. By understanding and using `@RequestParam`, you can create more flexible and robust web applications.

docs/java/generics/Generic Classes.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
id: generic-classes
3+
title: Generic Classes
4+
sidebar_label: Generic Classes
5+
sidebar_position: 2
6+
tags: [java, generics]
7+
description: In this tutorial, we will explore how to create and use generic classes in JAVA.
8+
---
9+
10+
# Generic Classes
11+
12+
- Generic classes in Java allow you to define classes with placeholders for types.
13+
14+
- These placeholders can then be replaced with actual types when creating instances of the class. This provides flexibility and type safety.
15+
16+
- To define a generic class, you use angle brackets `<>` to declare type parameters. These parameters can be used throughout the class definition.
17+
18+
```java
19+
// T is the placeholder representing the type of the item stored in the Box.
20+
public class Box<T> {
21+
private T item;
22+
23+
public void setItem(T item) {
24+
this.item = item;
25+
}
26+
27+
public T getItem() {
28+
return item;
29+
}
30+
}
31+
```
32+
33+
When you create an instance of `Box` and specify a type, such as `Box<Integer>`, T is replaced with `Integer`, and the `Box` class effectively becomes a container for integers.
34+
35+
```java
36+
public class Main {
37+
public static void main(String[] args) {
38+
Box<Integer> integerBox = new Box<>();
39+
integerBox.setItem(10);
40+
System.out.println("Item in the integer box: " + integerBox.getItem());
41+
42+
Box<String> stringBox = new Box<>();
43+
stringBox.setItem("Hello, World!");
44+
System.out.println("Item in the string box: " + stringBox.getItem());
45+
}
46+
}
47+
```
48+
49+
The above Java program demonstrates the usage of a generic class Box with two different types (`Integer` and `String`).

docs/java/generics/Generic methods.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
id: generic-methods
3+
title: Generic methods
4+
sidebar_label: Generic methods
5+
sidebar_position: 3
6+
tags: [java, generics]
7+
description: In this tutorial, we will explore explore how to create and use generic methods in JAVA.
8+
---
9+
10+
# Generic methods
11+
12+
- Generic methods allow you to create methods that work with different types without specifying the actual type until the method is called.
13+
14+
- The syntax for defining a generic method involves placing type parameters in angle brackets before the return type.
15+
16+
```java
17+
public class ArrayPrinter {
18+
// Generic method to print an array of any type
19+
public static <T> void printArray(T[] array) {
20+
for (T item : array) {
21+
System.out.print(item + " ");
22+
}
23+
System.out.println();
24+
}
25+
26+
public static void main(String[] args) {
27+
Integer[] intArray = {1, 2, 3, 4, 5};
28+
String[] stringArray = {"apple", "banana", "orange"};
29+
30+
// Print the Integer array
31+
System.out.print("Integer Array: ");
32+
printArray(intArray);
33+
34+
// Print the String array
35+
System.out.print("String Array: ");
36+
printArray(stringArray);
37+
}
38+
}
39+
```
40+
41+
- The above code is to demonstrate the use of a generic method printArray that can print elements of arrays of any type.

0 commit comments

Comments
 (0)