How to modify the servlet request

Introduction

Dealing with http web frameworks, one day or the other one has to cope requirements that go beyond the standard features offered by the chosen platform.  A common issue would be to change the request submitted by the client or the response content returned to it on the fly. Java Servlet technology deals with these issues basically with servlet filters and some other complementary trick the we will explain in the following paragraphs.

How to change the http request

Changing the servlet http request can  be done using the servlet filter mechanism, but that is not enough.  Most of the HttpRequest object fields are read-only ones, since the standard scenario does not cover the possibility that the original request information submitted by the client could be changed. The strategy to overcome this limitation is to wrap the request in another class, customize the wanted getter methods and submit to the filter chain the wrapper object instead of the original request. Java servlet API already comes with two classes named ServletRequestWrapper and HttpServletRequestWrapper that can be used as wrappers for the request.  In order to change the original request one has to create a class that extends one of these two classes, depending on what fields are need to be changed (if the field is available in the ServletRequest class just extending the ServletRequestWrapper will do). Then in this class one can overwrite the getter methods that provide the needed fields and implements the wanted logic to retrieve their custom values. Finally in the Servlet Filter doFilter method an instance of the wrapper class is created passing the original request in its constructor and then passed to the chain doFilter method call instead of the original request.

Http request change example

The following is a simple example of how the all think works:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

  ServletRequest requestModified = new HttpServletRequestWrapper(
  (HttpServletRequest) request) {
    @Override
    public String getParameter(String name) {
      String paramValue = super.getParameter(name);
      if(name.equals("parameterThatNeedsDefault") 
         && paramValue == null) {
        return "defaultValue";
      }
      return paramValue;
    }
  };

  chain.doFilter(requestModified, response);
}

In this example a default value is provided when a specific request parameter is found null. An anonimous inner class is used to extend the HttpServletRequestWrapper to make the implementation terser. The example above is trivial, nevertheless it shows just what is needed to hack into the servlet request lifecycle.

Transform the http response

In the following post  How to transform the servlet response content we will describe how to transform the response content just before it is sent back to the client.

How to modify the servlet request last modified: 2015-04-20T23:29:32+00:00 by Mario Casari

Leave a Reply

Your email address will not be published. Required fields are marked *