Java.Servlet.What servlet wrapper classes do you know?

🧠 What Are Servlet Wrapper Classes?

Wrapper classes are special decorators that wrap around the original HttpServletRequest, HttpServletResponse, or ServletInputStream/ServletOutputStream.

They allow you to:

  • Intercept or modify behavior
  • Read, change, or log request/response data
  • Implement security, compression, caching, etc.

In short: wrapper = original object + extra behavior (without modifying the original object itself).

🎯 Main Servlet Wrapper Classes (from Java EE / Jakarta EE)

Wrapper ClassWrapsPurpose
HttpServletRequestWrapperHttpServletRequestModify or read the incoming request
HttpServletResponseWrapperHttpServletResponseModify or decorate the outgoing response
ServletRequestWrapperServletRequest (more generic)Modify a basic servlet request
ServletResponseWrapperServletResponse (more generic)Modify a basic servlet response
ServletInputStream (custom wrapper)Servlet input streamIf you want to intercept raw request body
ServletOutputStream (custom wrapper)Servlet output streamIf you want to capture or compress response

🔥 Let’s See Quick Examples:

1. HttpServletRequestWrapper

Used when you want to override some methods from HttpServletRequest.

✅ Example:
Suppose you want to add a fake header to all requests.

public class MyRequestWrapper extends HttpServletRequestWrapper {
    public MyRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getHeader(String name) {
        if ("X-Fake-Header".equals(name)) {
            return "FakeHeaderValue";
        }
        return super.getHeader(name);
    }
}

Then inside a Filter:

MyRequestWrapper wrappedRequest = new MyRequestWrapper((HttpServletRequest) request);
chain.doFilter(wrappedRequest, response);

2. HttpServletResponseWrapper

Used when you want to capture or modify the outgoing response.

✅ Example:
Suppose you want to capture response content.

public class MyResponseWrapper extends HttpServletResponseWrapper {
    private StringWriter capture;

    public MyResponseWrapper(HttpServletResponse response) {
        super(response);
        capture = new StringWriter();
    }

    @Override
    public PrintWriter getWriter() {
        return new PrintWriter(capture);
    }

    public String getCapturedData() {
        return capture.toString();
    }
}

You can then analyze the response later in your filter.

3. ServletInputStream / ServletOutputStream Wrappers

These are lower-level and you use them if you want to:

  • Read raw POST body multiple times (normally it can only be read once).
  • Compress output (like GZIP).
  • Encrypt/decrypt stream data.

You would usually extend ServletInputStream or ServletOutputStream and override methods like read(), write(), etc.

🚀 Why Are Wrappers Useful?

  • Security: e.g., sanitize inputs (XSS protection).
  • Logging: log request and response bodies.
  • Compression: wrap response with GZIPOutputStream.
  • Modification: inject or change data on the fly.
  • Caching: cache results of slow operations.

You often use wrappers inside a Filter, because filters are perfect for request/response interception.


⚡ Quick Summary:

You NeedUse This Wrapper
Modify incoming requestHttpServletRequestWrapper
Modify outgoing responseHttpServletResponseWrapper
Modify raw input streamCustom ServletInputStream subclass
Modify raw output streamCustom ServletOutputStream subclass
Modify generic (non-HTTP) requests/responsesServletRequestWrapper / ServletResponseWrapper
This entry was posted in Без рубрики. Bookmark the permalink.