## What is State? **State** is information an application remembers, such as the selected patient or logged-in user. ## What is Stateless? A **stateless** component processes each request without relying on client information remembered from previous requests. Each request supplies the necessary inputs. **Example:** You send the **patient ID every time** you request that patient's details. ## What is Stateful? A **stateful** component remembers information between requests and uses it in later interactions. **Example:** You select a patient once. The application remembers the selection and uses it when you request the selected patient's details. ## Stateless versus Stateful | Feature | Stateless | Stateful | |---|---|---| | Previous client information | Does not rely on remembered information | Remembers information between requests | | Request inputs | Supplies the required context each time | Can reuse saved context | | Example | Send patient ID with every lookup | Remember the selected patient | | Multiple servers | Easier to distribute requests | Saved state must be available to the next server | | Database access | Can read and write data | Can read and write data | > **Note:** Stateless does not mean “no database.” Database records and temporary session information are different. ## Diagram This diagram compares two possible designs for viewing patient details. ```mermaid sequenceDiagram participant B as Browser participant S as Stateless service participant F as Stateful service B->>S: Get details using patient ID S-->>B: Patient details Note over S: No saved patient selection required B->>S: Send patient ID again S-->>B: Patient details B->>F: Select a patient Note over F: Remember selection in session F-->>B: Selection saved B->>F: Show selected patient's details F-->>B: Use remembered selection ``` ## Examples in HMIS - **Stateful behavior:** `SessionController` uses `@SessionScoped` to remember information such as the logged-in user and selected department. - **Stateless EJB:** `PatientFacade` uses `@Stateless` for database operations without retaining a client conversation. > `@SessionScoped` and `@Stateful` are different Java annotations. A controller can have stateful behavior without being an `@Stateful` EJB.