Common

UUID

HTTP Request

Cryptography

Common

Reflection

IO

WebUtils Tutorial

This tutorial provides an overview of the WebUtils class, which is part of the lightweight-component/aj-util library. The WebUtils class provides utility methods for web-related operations in Java applications.

Introduction

The WebUtils class contains static methods for common web operations such as IP address retrieval and cookie handling.

Main Features

Methods

1. getLocalIp()

Retrieves the local IP address of the server.

Example:

String ip = WebUtils.getLocalIp();

2. getClientIp(HttpServletRequest request)

Retrieves the client's IP address from an HTTP request, handling various proxy headers.

Example:

String clientIp = WebUtils.getClientIp(request);

3. getCookie(HttpServletRequest request, String cookieName)

Retrieves a cookie value by name from an HTTP request.

Example:

String sessionId = WebUtils.getCookie(request, "JSESSIONID");

Usage Example

Here's a complete example of using the WebUtils class:

import javax.servlet.http.HttpServletRequest;
import com.ajaxjs.util.WebUtils;

public class Example {
    public void processRequest(HttpServletRequest request) {
        // Get server IP
        String serverIp = WebUtils.getLocalIp();
        System.out.println("Server IP: " + serverIp);
        
        // Get client IP
        String clientIp = WebUtils.getClientIp(request);
        System.out.println("Client IP: " + clientIp);
        
        // Get session cookie
        String sessionId = WebUtils.getCookie(request, "JSESSIONID");
        System.out.println("Session ID: " + sessionId);
    }
}

Conclusion

The WebUtils class provides simple utility methods for common web operations, making it easier to handle IP addresses and cookies in Java web applications.