Simple Student API - Full Stack Project (No Frameworks)
Frontend: ReactJS
Backend: Pure Java HTTP Server (no Spring, no libraries)
Project Structure:
student-project/
├── backend/
│ └── StudentServer.java
└── frontend/
└── src/
└── App.js
===================================
Backend: StudentServer.java (Pure Java)
===================================
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.*;
import java.net.InetSocketAddress;
import java.util.*;
class Student {
String name;
int age;
String email;
Student(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
public String toJson() {
return String.format("{\"name\":\"%s\",\"age\":%d,\"email\":\"%s\"}", name, age, email);
public class StudentServer {
static List<Student> studentList = new ArrayList<>();
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/students", new StudentHandler());
server.setExecutor(null);
server.start();
System.out.println("Server started on port 8080");
static class StudentHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String method = exchange.getRequestMethod();
exchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
exchange.getResponseHeaders().set("Content-Type", "application/json");
if ("GET".equalsIgnoreCase(method)) {
StringBuilder response = new StringBuilder("[");
for (int i = 0; i < studentList.size(); i++) {
response.append(studentList.get(i).toJson());
if (i < studentList.size() - 1) response.append(",");
response.append("]");
byte[] bytes = response.toString().getBytes();
exchange.sendResponseHeaders(200, bytes.length);
OutputStream os = exchange.getResponseBody();
os.write(bytes);
os.close();
} else if ("POST".equalsIgnoreCase(method)) {
InputStream is = exchange.getRequestBody();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder body = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
body.append(line);
String json = body.toString();
String name = json.split("\"name\":\"")[1].split("\"")[0];
int age = Integer.parseInt(json.split("\"age\":")[1].split(",")[0]);
String email = json.split("\"email\":\"")[1].split("\"")[0];
studentList.add(new Student(name, age, email));
String res = "{\"status\":\"added\"}";
byte[] bytes = res.getBytes();
exchange.sendResponseHeaders(200, bytes.length);
OutputStream os = exchange.getResponseBody();
os.write(bytes);
os.close();
Run commands:
$ javac StudentServer.java
$ java StudentServer
===================================
Frontend: ReactJS - App.js
===================================
import React, { useEffect, useState } from 'react';
function App() {
const [students, setStudents] = useState([]);
const [form, setForm] = useState({ name: '', age: '', email: '' });
useEffect(() => {
fetch('http://localhost:8080/students')
.then(res => res.json())
.then(data => setStudents(data));
}, []);
const handleSubmit = (e) => {
e.preventDefault();
fetch('http://localhost:8080/students', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(form)
}).then(() => window.location.reload());
};
return (
<div style={{ padding: 20 }}>
<h2>Student Form</h2>
<form onSubmit={handleSubmit}>
<input placeholder="Name" onChange={e => setForm({ ...form, name: e.target.value
})} />
<input type="number" placeholder="Age" onChange={e => setForm({ ...form, age:
+e.target.value })} />
<input placeholder="Email" onChange={e => setForm({ ...form, email: e.target.value })}
/>
<button type="submit">Add</button>
</form>
<h3>Student List</h3>
<ul>
{students.map((s, i) => (
<li key={i}>{s.name} ({s.age}) - {s.email}</li>
))}
</ul>
</div>
);
export default App;
===================================
How to Run
===================================
1. Backend:
$ javac StudentServer.java
$ java StudentServer
2. Frontend:
$ npx create-react-app frontend
$ cd frontend
$ npm start
Replace App.js with the above code