The key to achieving custom output is to encapsulate httpservletresponse, intercept all outputs, and wait until the filter chain completes processing,
Filter. dofilter: captures the output for processing and writes it to the real httpservletresponse. Already exists in J2EE
Httpservletresponsewrapper makes it easier to wrap httpservletresponse
Public class gzipfilter implements filter {
Public void destroy (){
}
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception,
Servletexception {
Httpservletresponse resp = (httpservletresponse) response;
Wrapper wrapper = new wrapper (RESP );
Chain. dofilter (request, wrapper );
Byte [] gzipdata = gzip (wrapper. getresponsedata ());
Resp. addheader ("content-encoding", "gzip ");
Resp. setcontentlength (gzipdata. Length );
Servletoutputstream output = response. getoutputstream ();
Output. Write (gzipdata );
Output. Flush ();
}
Private byte [] gzip (byte [] data ){
Bytearrayoutputstream byteoutput = new bytearrayoutputstream (10240 );
Gzipoutputstream output = NULL;
Try {
Output = new gzipoutputstream (byteoutput );
Output. Write (data );
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (output! = NULL ){
Try {
Output. Close ();
} Catch (ioexception e ){
}
}
}
Return byteoutput. tobytearray ();
}
Public void Init (filterconfig fconfig) throws servletexception {
}
}
Wrapper class:
Public class wrapper extends httpservletresponsewrapper {
Public static final int ot_none = 0, ot_writer = 1, ot_stream = 2;
Private int outputtype = ot_none;
Private servletoutputstream output = NULL;
Private printwriter writer = NULL;
Private bytearrayoutputstream buffer = NULL;
Public wrapper (httpservletresponse resp) throws ioexception {
Super (RESP );
Buffer = new bytearrayoutputstream ();
}
Public printwriter getwriter () throws ioexception {
If (outputtype = ot_stream)
Throw new illegalstateexception ();
Else if (outputtype = ot_writer)
Return writer;
Else {
Outputtype = ot_writer;
Writer = new printwriter (New outputstreamwriter (buffer, getcharacterencoding ()));
Return writer;
}
}
Public servletoutputstream getoutputstream () throws ioexception {
If (outputtype = ot_writer)
Throw new illegalstateexception ();
Else if (outputtype = ot_stream)
Return output;
Else {
Outputtype = ot_stream;
Output = new wrappedoutputstream (buffer );
Return output;
}
}
Public void flushbuffer () throws ioexception {
If (outputtype = ot_writer)
Writer. Flush ();
If (outputtype = ot_stream)
Output. Flush ();
}
Public void reset (){
Outputtype = ot_none;
Buffer. Reset ();
}
Public byte [] getresponsedata () throws ioexception {
Flushbuffer ();
Return buffer. tobytearray ();
}
// Internal class, extended servletoutputstream
Class wrappedoutputstream extends servletoutputstream {
Private bytearrayoutputstream buffer;
Public wrappedoutputstream (bytearrayoutputstream buffer ){
This. Buffer = buffer;
}
Public void write (int B) throws ioexception {
Buffer. Write (B );
}
Public byte [] tobytearray (){
Return buffer. tobytearray ();
}
}
}