How to write response filter?
Is there a way to handle only response in a filter .
Is th开发者_如何学JAVAe code written below correct ?
public void doFilter(request , response , chain) {
//code to handle request
chain.doFilter(request, response);
//code to handle response .
}
It depends on what you want. In general, your sample is not correct though. After chain.doFilter
has returned, it's too late to do anything with the response. At this point, entire response was already sent to the client and your code has no access to it.
What you need to do is to wrap request
and/or response
into your own classes, pass these wrappers into doFilter
method and handle any processing in your wrappers.
To make it easier, there are already wrappers available in servlet api: see HttpServletRequestWrapper
and HttpServletResponseWrapper
classes. If you want to process output that is actually sent to client, you also need to write custom OutputStream or Writer wrappers, and return those from your HttpServletResponse wrapper. Yeah, lot of wrapping :)
Some simpler filters can work without wrapping request or response: e.g. before calling doFilter
, you can already access request headers or you can send custom response without calling doFilter
. But if you want to process request body, you cannot just read it, otherwise it won't be available to the rest of the chain. In this case you need to use the wrapping technique again.
The code you show is not entirely correct, but with a necessary simplification of the terminology - it is. You can "handle" a request even after chain.doFilter(..)
(and response before it).
What chain.doFilter(..)
means is that the process is passed to the desired target, and when the method returns, the target has completed its output.
So to be more precise - it is 'before' and 'after' the request has been processed and a response - generated.
Your code seems to be fine.
If you only want to handle the response, you can just put your code in the //Code to handle response .
section and do whatever you like.
If you want to do something with the output, you will have to provide a special response wrapper that handles the outputstream in the response where the servlet (and other filters) might write to.
Request and Responses are readonly.So they dont have setter methods to modify the contents of that.But by using the "HttpServletRequestWrapper and HttpServletResponseWrapper" classes which are provided built in by java we can modify its content.We are encapsulating the original request and response objects by the wrapper objects,by modifying the wrapper objects we can really modify the original request and response objects.
I filtered the response by using below set of code :
public void doFilter(request , response , chain) {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
CharResponseWrapper responseWrapper = new CharResponseWrapper(httpServletResponse);
ServletResponse newResponse = response;
if (request instanceof HttpServletRequest) {
newResponse = new CharResponseWrapper((HttpServletResponse) response);
}
chain.doFilter(request, newResponse);
if (newResponse instanceof CharResponseWrapper) {
String responseString = newResponse.toString();
if(StringUtils.isNotBlank(walletResponseString)) {
//code to handle response by converting to object
}
}
}
精彩评论