Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to remove a HttpServletRequest parameter in a controller/class?

Tags:

jsp

servlets

I have a case where I need to redirect my HTTP request object to other controllers/classes for further processing. The problem is that in some controller, I would like to get better control on the parameters I'm forwarding to the next class: modify, edit, remove them. So, I would like to know if there is a good practice/pattern to achieve this basic control on the HTTP request parameters.

like image 304
work.paul Avatar asked Oct 15 '10 07:10

work.paul


People also ask

Which method of the HttpServletRequest object is used if there is a chance of retrieving multiple values for any given input?

If there's any chance a parameter could have more than one value, you should use the getParameterValues( ) method instead.

What are HttpServletRequest parameters?

Parameters. The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.

How do I pass HttpServletRequest in Java?

Pass it to the constructor: public class XmlParser{ final private HttpServletRequest request; public XmlParser(HttpServletRequest request) { this. request = request; } // use it in othe methods... }

Is HttpServletRequest a class or interface?

Extends the ServletRequest interface to provide request information for HTTP servlets. The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods ( doGet , doPost , etc). String identifier for Basic authentication.


2 Answers

The good practice is to wrap the request object in another object using a servlet filter. Since HttpServletRequest is an interface, you can write your own implementation of it. Your implementation can hold the request you received and delegate any and all of its own methods to the original request object, but also modify the return values as you see fit. So your getParameter() etc. methods could call the same method on the original request object, and modify the result as you see fit before returning it.

class MyHttpServletRequestWrapper implements HttpServletRequest {
   private HttpServletRequest originalRequest;

   public MyHttpServletRequestWrapper(HttpServletRequest originalRequest) {
      this.originalRequest = originalRequest;

   public String getAuthType() {return originalRequest.getAuthType();}
   public String getQueryString() {return originalRequest.getQueryString();}
   // etc.

   public Map getParameterMap() {
      Map params = originalRequest.getParameterMap();
      params.remove("parameter-to-remove");
      params.put("parameter-to-add", "<a value>");
      //etc.
   }
}

Your servlet filter:

class MyFilter implements Filter {
    public void init(FilterConfig config) {
       // perhaps you might want to initialize something here
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        HttpServletRequest originalRequest = (HttpServletRequest) request;
        HttpServletRequest newRequest = new MyHttpServletRequest(originalRequest);
        chain.doFilter(newRequest, response);
    }
}

You can also subclass javax.servlet.request.HttpServletRequestWrapper, which will save you a bunch of work.

See this post for more.

like image 95
Ladlestein Avatar answered Oct 03 '22 10:10

Ladlestein


If you're after a simple one-liner, this regex technique worked for me:

myURL = myURL.replaceAll("[&?]clear=([^&]$|[^&]*)", "");

If you need it in Javascript, it's very similar indeed - which is nice!

var myUrl = (""+window.location).replace(/&?clear=([^&]$|[^&]*)/i, "");

clear is the name of the parameter to be removed.

like image 43
Dave Avatar answered Oct 03 '22 10:10

Dave