Best Practices

Server-Side Event Handling Best Practices

Learn patterns and anti-patterns for handling browser events in your Oorian applications.

M. WarbleFebruary 19, 20263 min read
Server-Side Event Handling Best Practices

Oorian's event system is one of its most powerful features. Events fired in the browser are handled by your Java code on the server. This article covers best practices to make your event handling clean, efficient, and maintainable.

The Listener Pattern

Oorian uses the same listener pattern as Swing and JavaFX. Implement a listener interface and register for events:

public class MyPage extends HtmlPage implements MouseClickListener, InputListener
{
    @Override
    public void onEvent(MouseClickedEvent event)
    {
        // Handle clicks
    }

    @Override
    public void onEvent(InputChangeEvent event)
    {
        // Handle input changes
    }
}

Best Practice #1: Identify the Source

When handling events from multiple components, always check the event source:

private Button saveButton;
private Button cancelButton;
private Button deleteButton;

@Override
public void onEvent(MouseClickedEvent event)
{
    Object source = event.getSource();

    if (source == saveButton)
    {
        handleSave();
    }
    else if (source == cancelButton)
    {
        handleCancel();
    }
    else if (source == deleteButton)
    {
        handleDelete();
    }
}

Best Practice #2: Keep Handlers Focused

Event handlers should be thin dispatchers, not business logic containers:

// Good: Thin handler that delegates
@Override
public void onEvent(FormEvent event)
{
    Parameters params = event.getParameters();
    String action = params.getParameterValue("action");

    switch (action)
    {
        case "create":
            createCustomer(params);
            break;
        case "update":
            updateCustomer(params);
            break;
        case "delete":
            deleteCustomer(params);
            break;
    }
}

// Bad: Business logic in handler
@Override
public void onEvent(FormEvent event)
{
    Parameters params = event.getParameters();
    String name = params.getParameterValue("name");
    String email = params.getParameterValue("email");
    // 50 lines of validation and database code...
}

Best Practice #3: Validate Early

Validate input at the start of your handler and return early if invalid:

@Override
public void onEvent(FormEvent event)
{
    Parameters params = event.getParameters();

    // Validate required fields
    String name = params.getParameterValue("name");
    if (name == null || name.trim().isEmpty())
    {
        showError("Name is required");
        return;
    }

    String email = params.getParameterValue("email");
    if (!isValidEmail(email))
    {
        showError("Invalid email address");
        return;
    }

    // Proceed with valid data
    processForm(name, email);
}

Best Practice #4: Handle Exceptions Gracefully

Wrap handler logic in try-catch to prevent uncaught exceptions from breaking the UI:

@Override
public void onEvent(MouseClickedEvent event)
{
    try
    {
        performOperation();
        showSuccess("Operation completed");
    }
    catch (BusinessException e)
    {
        showError(e.getMessage());
    }
    catch (Exception e)
    {
        logger.error("Unexpected error", e);
        showError("An unexpected error occurred");
    }
}

Best Practice #5: Use Appropriate Event Types

Choose the right event for your needs:

Event Use Case
MouseClickedEvent Button clicks, link clicks, icon clicks
InputChangeEvent Text input as user types (debounced)
InputCompleteEvent When user finishes editing (blur/enter)
FormEvent Complete form submissions
KeyPressedEvent Keyboard key presses

Anti-Pattern: Event Handler Bloat

Avoid putting too much logic in a single page class. Extract to services:

// Bad: Page does everything
public class CustomerPage extends HtmlPage
{
    // 500 lines of event handlers and business logic
}

// Good: Page delegates to services
public class CustomerPage extends HtmlPage
{
    private final CustomerService customerService;
    private final ValidationService validationService;

    @Override
    public void onEvent(FormEvent event)
    {
        Customer customer = extractCustomer(event);
        validationService.validate(customer);
        customerService.save(customer);
        refreshUI();
    }
}

Conclusion

Clean event handling makes your Oorian applications maintainable and robust. Keep handlers thin, validate early, handle exceptions gracefully, and delegate business logic to services.

Related Articles

Security

Security by Default: How Oorian Protects Your Applications

January 11, 2026
Announcement

Why We Built Oorian: The Story Behind the Framework

January 7, 2026
Tutorial

Getting Started with Oorian: Your First Java Web Application

December 31, 2025