jsp - Removing Client Response Url parameter -
question. when having client url following parameters contactvia=email&interestedin=petrock&terms=on
how can 1 remove (present) attribute terms=on
when call reaches servlet?
so response url becomes contactvia=email&interestedin=petrock
context. values in textboxes calling form automatically added response url, have tried remove terms request.removeattribute();
, editing request.getparametermap();
.
but removeattribute has no effect , map immutable. suggestions?
this kind of scenario 1 of reasons javax.servlet.http.httpservletrequestwrapper exists. can subclass , override behaviour.
a javax.servlet.filter place this:
@webfilter(urlpatterns = "/same/as/your/servlet") public class parameterfilter implements filter { /* * treat guide - it's not tested in way */ @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { if (request instanceof httpservletrequest) { final httpservletrequestwrapper wrappedrequest = new httpservletrequestwrapper((httpservletrequest) request) { @override public map<string, string[]> getparametermap() { return request.getparametermap() .entryset() .stream() .filter(mapentry -> !mapentry.getkey().equals("terms")) .collect(collectors.tomap(mapentry -> mapentry.getkey(), mapentry -> mapentry.getvalue())); } @override public enumeration<string> getparameternames() { return new vector(request.getparametermap() .keyset() .stream() .filter(key -> !key.equals("terms")) .collect(collectors.tolist())).elements(); } @override public string getparameter(string name) { return name.equals("terms") ? null : super.getparameter(name); } @override public string[] getparametervalues(string name) { return name.equals("terms") ? null : super.getparametervalues(name); } @override public string getquerystring() { final string[] querytuples = super.getquerystring().split("&"); return string.join("&", arrays.stream(querytuples) .filter(pv -> !pv.matches("terms=?")) .collect(collectors.tolist()).toarray(new string[querytuples.length])); } }; chain.dofilter(wrappedrequest, response); } else { chain.dofilter(request, response); } } @override public void init(filterconfig filterconfig) throws servletexception { } @override public void destroy() { } }
you can make more or less elaborate depending upon needs.
Comments
Post a Comment