java 如何编写响应过滤器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5634477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write response filter?
提问by Vinoth Kumar C M
Is there a way to handle only response in a filter .
有没有办法只处理过滤器中的响应。
Is the code written below correct ?
下面写的代码正确吗?
public void doFilter(request , response , chain) {
//code to handle request
chain.doFilter(request, response);
//code to handle response .
}
回答by Peter ?tibrany
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.
这取决于你想要什么。一般来说,你的样本是不正确的。之后chain.doFilter
又回来了,这是来不及做的任何回应。此时,整个响应已经发送到客户端,您的代码无法访问它。
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.
您需要做的是包装request
和/或response
放入您自己的类中,将这些包装器传递给doFilter
方法并处理包装器中的任何处理。
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 :)
为方便起见,servlet api 中已经有可用的包装器:seeHttpServletRequestWrapper
和HttpServletResponseWrapper
classes。如果您想处理实际发送到客户端的输出,您还需要编写自定义的 OutputStream 或 Writer 包装器,并从您的 HttpServletResponse 包装器中返回它们。是的,很多包装:)
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.
一些更简单的过滤器可以在不包装请求或响应的情况下工作:例如,在调用之前doFilter
,您已经可以访问请求标头,或者您可以发送自定义响应而无需调用doFilter
。但是如果你想处理请求体,你不能只读取它,否则链的其余部分将无法使用它。在这种情况下,您需要再次使用包装技术。
回答by Bozho
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).
您显示的代码并不完全正确,但对术语进行了必要的简化 - 确实如此。您甚至可以在之后chain.doFilter(..)
(并在其之前响应)“处理”请求。
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.
什么chain.doFilter(..)
意思是,该方法被传递到期望的目标,并且当该方法返回,则目标已经完成了其输出。
So to be more precise - it is 'before' and 'after' the request has been processed and a response - generated.
因此,更准确地说——它是在请求被处理和响应被“之前”和“之后”生成的。
回答by chinnu
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.
请求和响应是只读的。所以他们没有setter方法来修改内容。但是通过使用java内置的“HttpServletRequestWrapper和HttpServletResponseWrapper”类我们可以修改它的内容。我们封装了原始的请求和响应对象由包装对象,通过修改包装对象,我们可以真正修改原始请求和响应对象。
回答by Sanchi Girotra
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
}
}
}
回答by csupnig
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.
如果您只想处理响应,您可以将您的代码放在该//Code to handle response .
部分并做您喜欢的任何事情。
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.
如果您想对输出执行某些操作,则必须提供一个特殊的响应包装器来处理 servlet(和其他过滤器)可能写入的响应中的输出流。